PasswordBox用法

本文介绍如何使用WPF实现简单的注册功能,包括界面设计及密码安全处理。通过PasswordBox控件收集用户输入,并利用.NET框架提供的安全方法进行密码转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 

最近研究一下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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值