private async void Submit_Click(object sender, EventArgs e) //登陆验证 { string username = this.UserName.Text.Trim(); string password = this.PassWord.Text.Trim(); if (username == "") { errormessage.IsBalloon = true; errormessage.SetToolTip(this.UserName, "请填写用户名后再登录"); errormessage.Show("请填写用户名后再登录", this.UserName, 1, this.UserName.Height / 2, 2000); errormessage.UseFading = false; UserName.Focus(); return; } else if (password == "") { errormessage.IsBalloon = true; errormessage.SetToolTip(this.PassWord, "请填写密码后再登录"); errormessage.Show("请填写密码后再登录", this.PassWord, 1, this.PassWord.Height / 2, 2000); errormessage.UseFading = false; PassWord.Focus(); return; } else { User user = new User(); //BUser bUser = new BUser(); FileStream fs = new FileStream("data.bin", FileMode.Create); BinaryFormatter bf = new BinaryFormatter(); user.Username = username; if (this.RemeberPassword.Checked) // 如果单击了记住密码的功能 { // 在文件中保存密码 user.Password = password; } else { // 不在文件中保存密码 user.Password = ""; } // 选在集合中是否存在用户名 if (users.ContainsKey(user.Username)) { users.Remove(user.Username); } users.Add(user.Username, user); //要先将User类先设为可以序列化(即在类的前面加[Serializable]) bf.Serialize(fs, users); //user.Password = this.PassWord.Text; fs.Close(); try { _sc = new SignSocketClient(); cmd.ShowOpaqueLayer(AllForm, 125, true, true, "正在登录"); Employee emp = new Employee(); emp = await _sc.Login(username, password); if (emp != null) { UserHelper.UserInfo = emp; this.DialogResult = DialogResult.OK; cmd.HideOpaqueLayer(); this.Close(); Logging.AddLog("用户:" + emp.Name + "登陆成功!"); } else { cmd.HideOpaqueLayer(); PassWord.Text = ""; errormessage.IsBalloon = true; errormessage.SetToolTip(this.PassWord, "用户名与密码不匹配"); errormessage.Show("用户名与密码不匹配", this.PassWord, 1, this.PassWord.Height / 2, 2000); errormessage.UseFading = false; PassWord.Focus(); return; } } catch { MessageBox.Show("无法连接服务器", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } }