C# WindowsForm 员工管理系统六【修改密码】

修改密码的界面相信大家都很熟悉了,这里唯一需要注意的是“修改密码”功能放在登录界面上还是放在主界面上,如果是在登录界面上,则修改密码界面初始化时并没有用户信息,而在主界面时,我们可以得到用户信息,限制只能修改当前用户密码。这里我们将“修改密码”功能置于主界面上。

创建PasswordChange窗体

C# WindowsForm 员工管理系统六【修改密码】_第1张图片

如果希望用户名锁定当前用户,不能修改,则可以将TextBox的readonly属性设置为true,并在窗体加载是将用户名写在TextBox上。

        private void PasswordChange_Load(object sender, EventArgs e)
        {
            string currentUser = Form1.UserName;
            txtName.Text = currentUser;
        }

双击“修改”按钮,添加事件处理代码

 private void Change_Click(object sender, EventArgs e)
        {
            string connstr = ConfigurationManager.ConnectionStrings["WindowsFormsApplication6.Properties.Settings.staffConnectionString"].ConnectionString;
            SqlConnection conn = new SqlConnection(connstr);
            string sql = "select Password from StaffAccount where Name ='" + txtName.Text + "'";
            SqlCommand cmd = new SqlCommand(sql, conn);
            conn.Open();
            SqlDataReader sdr = cmd.ExecuteReader();
            if (sdr.Read())
            {
                string oldPwd = sdr.GetString(0).Trim();
                 if (oldPwd == txtOldPwd.Text)
                {
                    if (txtNewPassword.Text.Trim() == "" || txtNewPwdAgain.Text.Trim() == "")
                    {
                    MessageBox.Show("新密码确认不能为空!");
                    return;
                    }
                    else if (txtNewPassword.Text.Trim() != txtNewPwdAgain.Text.Trim())
                    {
                        MessageBox.Show("2次输入的新密码不一样,请重新输入!");
                        txtNewPassword.Text = "";
                        txtNewPwdAgain.Text = "";
                        return;
                    }
                    else
                    {
                        sdr.Close();
                        string sqlUpdate = "update StaffAccount set Password ='" + txtNewPwdAgain.Text +
                        "' where Name ='" + txtName.Text + "'";
                        SqlCommand cmdUp = new SqlCommand(sqlUpdate, conn);
                        if (cmdUp.ExecuteNonQuery() == 0)
                        {
                            MessageBox.Show("未知错误!");
                            return;
                        }
                        else
                        {
                            MainForm mainForm = new MainForm();
                            mainForm.Show();
                            this.Close();
                            MessageBox.Show("恭喜你!密码修改成功!");
                        }
                    }
                }
                else
                {
                    MessageBox.Show("旧密码错误或者不能为空");
                    txtOldPwd.Text = "";
                    txtNewPassword.Text = "";
                    txtNewPwdAgain.Text = "";
                    return;
                }
            }
            else
            {
                MessageBox.Show("用户名不存在,请重新输入!");
                txtName.Text = "";
                txtOldPwd.Text = "";
                txtNewPassword.Text = "";
                txtNewPwdAgain.Text = "";
                return;             
            }
            conn.Close();
        }

这里输入错误提示用的是MessageBox,也可以采用“用户注册”窗体用的Label控件。

双击“返回”按钮

        private void Back_Click(object sender, EventArgs e)
        {
            this.Close();
        }

双击MainForm窗体的“修改密码”按钮

            PasswordChange PwdChange = new PasswordChange();
            PwdChange.Show();

这样员工管理系统基本已经完成了。最后写两种如何在PictureBox上显示Label内容的方法。
添加PictureBox的Paint事件

private void pictureBox2_Paint(object sender, PaintEventArgs e)
        {
            PictureBox pb = sender as PictureBox;
            foreach (Control C in pb.Controls)
            {
                if (C is Label)
                {
                    Label L = (Label)C;
                    L.Visible = false;
                    e.Graphics.DrawString(L.Text, L.Font, new SolidBrush(L.ForeColor),
                        new RectangleF(L.Left - pb.Left, L.Top - pb.Top, L.Width, L.Height));
                }
            }
        }

添加Panel控件包含Label和PictureBox,并在窗体加载时执行一下代码

        //lblIdentify,lblWelcome为Label的名字
            lblIdentify.Parent = pictureBox2;
            lblWelcome.Parent = pictureBox2;

你可能感兴趣的:(C#,WinForm,密码,管理,员工,界面)