C# winform 登录窗体 记住密码的实现

Winform实现登录窗体中记住密码的功能,如图:

  

 

代码如下:

1、窗体Form1加载:

       List<UserInfo> item=new List<UserInfo>();
        UserInfo userInfo = new UserInfo();
        BUserInfo bUserInfo = new BUserInfo();
        Dictionary<string, UserInfo> users = new Dictionary<string, UserInfo>();
        private void Form1_Load(object sender, EventArgs e)
        {
            FileStream fs = new FileStream("data.bin", FileMode.OpenOrCreate);

            if (fs.Length > 0)
            {
                BinaryFormatter bf = new BinaryFormatter();
                users = bf.Deserialize(fs) as Dictionary<string, UserInfo>;
                foreach (UserInfo user in users.Values)
                {
                    comboBox1.Items.Add(user.UserName);
                }

                for (int i = 0; i < users.Count; i++)
                {
                    if (comboBox1.Text != "")
                    {
                        if (users.ContainsKey(comboBox1.Text))
                        {
                            textBox2.Text = users[comboBox1.Text].UserPwd;
                            checkBox1.Checked = true;
                        }
                    }
                }
            }
            fs.Close();


            item = (List<UserInfo>)bUserInfo.GetAll();
            comboBox1.DataSource = item;
            comboBox1.DisplayMember = "UserName";
        }

 

2、选择用户名:
        private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            FileStream fs = new FileStream("data.bin", FileMode.OpenOrCreate);

            if (fs.Length > 0)
            {
                BinaryFormatter bf = new BinaryFormatter();
                users = bf.Deserialize(fs) as Dictionary<string, UserInfo>;
                for (int i = 0; i < users.Count; i++)
                {
                    if (comboBox1.Text != "")
                    {
                        if (users.ContainsKey(comboBox1.Text) && users[comboBox1.Text].UserPwd != "")
                        {
                            textBox2.Text = users[comboBox1.Text].UserPwd;
                            checkBox1.Checked = true;
                        }
                        else
                        {
                            textBox2.Text = "";
                            checkBox1.Checked = false;
                        }
                    }
                }
            }

            fs.Close();
        }

 

 3、点击登录控件button1事件:
  private void button1_Click(object sender, EventArgs e)
        {
            UserInfo user = new UserInfo();
            BUserInfo bUser = new BUserInfo();
            FileStream fs = new FileStream("data.bin", FileMode.Create);
            BinaryFormatter bf = new BinaryFormatter();
            user.UserName = comboBox1.Text;
            if (checkBox1.Checked)
            {
                user.UserPwd = textBox2.Text.Trim();
            }
            else
            {
                user.UserPwd = "";
            }
            if (users.ContainsKey(user.UserName))
            {
                users.Remove(user.UserName);
            }
            users.Add(user.UserName, user);
            bf.Serialize(fs, users);
            user.UserPwd= textBox2.Text;
            int i = bUser.Login(user);
            if (i > 0)
            {
               
                Form2 f = new Form2();
                f.Show();
                this.Hide();
            }
            else
            {
                MessageBox.Show("登录失败!");
            }
            fs.Close();
        }

转载于:https://www.cnblogs.com/ou444/archive/2011/09/13/2174911.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值