C#记住密码功能实现

来源:互联网 发布:企业管理短信群发软件 编辑:程序博客网 时间:2024/05/06 06:48

无聊玩玩的小功能,希望给初学者带来一点点的激情,实现记住密码功能

首先创建一个类User.cs(用来编辑和获取用户名和密码)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace 记住密码
{
    [Serializable]
    public class User
    {
        //#region 用户名
        //private string loginId;
        //public string LoginID
        //{
        //    get { return loginId; }
        //    set { loginId = value; }
        //}
        //#endregion


        //#region 用户密码
        //private string pwd;
        //public string Pwd
        //{
        //    get { return pwd; }
        //    set { pwd = value; }
        //}
        //#endregion


        public string LoginID { get; set; }
        public string Pwd { get; set; }
        
    }
}

接着创建一个窗体Form1.cs(显示和保存)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;




namespace 记住密码
{
    public partial class Form1 : Form
    {
        Dictionary<string, User> dic = new Dictionary<string, User>();
        private int len =0;
        public Form1()
        {
            InitializeComponent();
        }


        private void button1_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定要退出吗?", "系统温馨提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            { 
                this.Close();
            }
            
        }
      
        #region 窗体加载
        /// <summary>
        /// 窗体加载的时候获取data里面的账号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Load(object sender, EventArgs e)
        {
            
            User u = new User();
            FileStream fs = new FileStream("data.txt", FileMode.OpenOrCreate);
            if (fs.Length > 0)
            {
                BinaryFormatter bf = new BinaryFormatter();
                dic = bf.Deserialize(fs) as Dictionary<string,User>;
                foreach (var User in dic.Values)
                {
                    comboBox1.Items.Add(User.LoginID);
                }
                if (comboBox1.Items.Count > 0)
                {
                    comboBox1.SelectedIndex = comboBox1.Items.Count - 1;
                }
            }
            fs.Close();
            len = Convert.ToInt32( comboBox1.Text.Length);
        }
        #endregion
        #region 登录按钮事件
        /// <summary>
        /// 单击按钮的时候判断是否保存用户信息,根据checkBox的chenck判断
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            User u = new User();
            FileStream fs = new FileStream("data.txt", FileMode.Create);//文件创建
            BinaryFormatter bf = new BinaryFormatter();//序列化
            u.LoginID  = comboBox1.Text.Trim();
            if (checkBox1.Checked)
            {
                u.Pwd = textBox1.Text.Trim();
            }
            else
            {
                u.Pwd = "";
            }
            //判断字典中是否有相同的用户名,如果有,就移除字典中的用户名
            if (dic.ContainsKey(u.LoginID))
            {
                dic.Remove(u.LoginID);
            }
            dic.Add(u.LoginID, u);
            bf.Serialize(fs, dic);
            fs.Close();
            
            #region  
            //把保存的用户显示在控件中
            FileStream Openfs = new FileStream("data.txt", FileMode.OpenOrCreate);
            if (Openfs.Length > 0)
            {
                dic = bf.Deserialize(Openfs) as Dictionary<string, User>;


                comboBox1.Items.Add(comboBox1.Text);


                if (comboBox1.Items.Count > 0)
                {
                    comboBox1.SelectedIndex = comboBox1.Items.Count - 1;
                }
            }
            Openfs.Close();
            #endregion
            
            MessageBox.Show("登录成功!");


        }
        #endregion
        #region 验证字典中是否有相同的用户名
        /// <summary>
        /// 
        /// </summary>
        public  void DisplayUserInfo()
        {
            
            string key = comboBox1.Text.Trim();
            if (dic.ContainsKey(key)==false)
            {
                return;
            }
            User u = dic[key];
            textBox1.Text = u.Pwd.Trim();
            checkBox1.Checked = comboBox1.Text.Trim().Length > 0 ? true : false;
        }
        #endregion




        #region 更改属性值的时候触发
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            DisplayUserInfo();
        }
        #endregion


        private void comboBox1_TextChanged(object sender, EventArgs e)
        {


            if (len != Convert.ToInt32(comboBox1.Text.Length))
            {
                textBox1.Text = "";
                DisplayUserInfo();
                
            }
            else
            {
                DisplayUserInfo();
            }
            
            
        }
    }
}


0 0
原创粉丝点击