C#winform登陆注册

发布于:2022-12-17 ⋅ 阅读:(274) ⋅ 点赞:(0)

一、登陆窗体

List<hb_userModel> _list = null;
        private hb_userBLL _userBLL = new hb_userBLL();
        private Point mPoint;
        private hb_userModel model = new hb_userModel();
        hb_userModel mod = new hb_userModel();  

/// <summary>
        /// 登录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLogin_Click(object sender, EventArgs e)
        {
            Login(this.txtUserName.Text.Trim(), this.txtPassword.Text.Trim());
        }

/// <summary>
        /// 登录方法
        /// </summary>
        /// <param name="Username"></param>
        /// <param name="Userpassword"></param>

private void Login(string Username, string Userpassword)
        {
            #region MyRegion
            if (!string.IsNullOrEmpty(txtUserName.Text) && !string.IsNullOrEmpty(txtPassword.Text))
            {
                _list = _userBLL.VaildId(Username, Userpassword).ToList();
                if (_list.Count > 0)
                {
                    this.Hide();
                    List<hb_userModel> user = _userBLL.Get(Username).ToList();
                    if (user.Count > 0)
                    {
                        foreach (hb_userModel item in user)
                        {
                            item.password = txtPassword.Text.Trim();
                            break;
                        }
                    }
                    MessageBox.Show("登陆成功!");
                }
                else
                {
                    errorProvider1.SetError(txtUserName, "用户名错误,请检查后再次输入!");
                    errorProvider1.SetError(txtPassword, "密码错误,请检查后再次输入!");
                }
            }
            else
            {
                if (string.IsNullOrEmpty(txtUserName.Text))
                {
                    errorProvider1.SetError(txtUserName, "用户账号不能为空!");
                }
                if (string.IsNullOrEmpty(txtPassword.Text))
                {
                    errorProvider1.SetError(txtPassword, "密码不能为空!");
                }
            }
            #endregion

}

二、注册窗体

public void Selects()
        {

            List<hb_userModel> lst = new List<hb_userModel>();
            List<hb_userModel> _list = _userBLL.Get(txtUserName.Text.Trim()).ToList();
            foreach (hb_userModel item in _list)
            {
                mod.id = item.id;
                mod.username = item.username;
                mod.password = item.password;
                mod.administrator = item.administrator;
                lst.Add(mod);
            }
        }
        public void ModelClass()
        {
            model.id = txtid.Text;
            model.username = txtUserName.Text;
            model.password = txtPassword.Text;
            model.administrator = cmbAdministrator.Text;
        }

 private void btnRegister_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtUserName.Text) && !string.IsNullOrEmpty(txtPassword.Text))
            {
                Selects();
                if (mod.username != txtUserName.Text)
                {
                    if (txtPasswords.Text == txtPassword.Text)
                    {
                        ModelClass();
                        model.id = Guid.NewGuid().ToString();
                        _userBLL.Insert(model);
                        MessageBox.Show("注册成功!");
                        LoginForm logon = new LoginForm();
                        this.Hide();
                        logon.ShowDialog();
                    }
                    else
                    {
                        txtPassword.Text = "";
                        txtPasswords.Text = "";
                        MessageBox.Show("密码不一致,请重新输入!");
                    }
                }
                else
                {
                    errorProvider1.SetError(txtUserName, "用户名已存在!请重新输入!");
                }
            }
            else
            {
                if (string.IsNullOrEmpty(txtUserName.Text))
                {
                    errorProvider1.SetError(txtUserName, "用户账号不能为空!");
                }
                if (string.IsNullOrEmpty(txtPassword.Text))
                {
                    errorProvider1.SetError(txtPassword, "密码不能为空!");
                }
                if (string.IsNullOrEmpty(txtPasswords.Text))
                {
                    errorProvider1.SetError(txtPasswords, "密码不能为空!");
                }
            }
        }


网站公告

今日签到

点亮在社区的每一天
去签到