代码如下:
1、窗体Form1加载:
List
if (fs.Length > 0) { BinaryFormatter bf = new BinaryFormatter(); users = bf.Deserialize(fs) as Dictionary
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
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
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(); }
最新评论