WPF之PasswordBox用法
最近研究一下wpf ,wpf確實很強大、很炫, 簡單做個註冊功能,下面用到了 passwordBox 控制元件
前臺程式碼
<Window x:Class="Main.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="註冊" Height="350" Width="530" WindowStartupLocation="CenterScreen" ResizeMode="NoResize"> <Grid > <Label Width="70" Height="30" Content="登陸姓名:" Margin="115,93,323,188" /> <TextBox Background="Yellow" Name="txtName" Margin="205,101,114,191" FontSize="20" /> <Label Content="登陸密碼:" Height="30" Margin="115,148,323,133" Width="70" /> <PasswordBox HorizontalAlignment="Left" Margin="205,148,0,141" Name="txtPwd" FontSize="20" Width="202" Background="Yellow" /> <Button Content="註冊" Background="GreenYellow" Name="btnLogin" Height="30" Width="60" Margin="205,222,243,59" Click="btnLogin_Click" /> <Button Background="GreenYellow" Content="重置" Height="30" Margin="334,222,114,59" Name="btnReset" Width="60" />
登陸按鈕裡面的事件
/// <summary> /// 登陸事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnLogin_Click(object sender, RoutedEventArgs e) { if (this.txtName.Text.Trim().ToString() != "" && this.txtPwd.SecurePassword.ToString().Trim() != "") { userInfo info = new userInfo(); info.UserName = this.txtName.Text.Trim().ToString();
// 使用一個IntPtr型別值來儲存加密字串的起始點 IntPtr p = System.Runtime.InteropServices.Marshal.SecureStringToBSTR(this.txtPwd.SecurePassword); // 使用.NET內部演算法把IntPtr指向處的字元集合轉換成字串 string password = System.Runtime.InteropServices.Marshal.PtrToStringBSTR(p); info.UserPwd = password; userInfoManager um = new userInfoManager(); int count = um.AdduserInfo(info); if (count > 0) { MessageBox.Show("註冊成功!"); } else { MessageBox.Show("註冊失敗!"); } } else { MessageBox.Show("使用者名稱或密碼為空!"); }
}
</Grid></Window>
---------------------
作者:阿呆0123
來源:CSDN
原文:https://blog.csdn.net/pengfeihe0123/article/details/5985219
版權宣告:本文為博主原創文章,轉載請附上博文連結!