一.任务目标
1.完成如下界面的制作,并能进行简单的密码验证
2.当用户输入的用户名或密码错误时,提示错误信息
3.当用户名和密码正确,进入主界面
image.png
二.任务要求
- 登录窗口出现在屏幕正中央,并且不能放大缩小
- 默认角色为“收银员”,并且只允许选择“收银员”和“库管员”两种角色
- 用户名最大长度不超过9个字符,密码需要显示为“*”号
- 登录正确则提示成功;登录失败则提示错误,注意使用错误图标
- 点击“退出”时退出应用程序
三.控件参数
1.PictureBox
image.png
2.Form1
image.png
3.Label
image.png
image.png
4.TextBox
image.png
5.ComboBox
image.png
//双击窗体时,设置默认角色为“收银员”
private void Form1_Load(object sender, EventArgs e)
{
this.comboBox1.SelectedIndex = 0;
}
6.LinkLable
image.png
7.Button
image.png
image.png
8.登录设置
image.png
image.png
//点击“登录”按钮则登录系统
private void button1_Click(object sender, EventArgs e)
{
if (this.comboBox1.SelectedItem.ToString() == "收银员")
{
if (this.textBox1.Text == "admin" && this.textBox2.Text == "123456")
{
MessageBox.Show("收银员登录成功");
}
else
{
MessageBox.Show("用户名或密码错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
if (this.comboBox1.SelectedItem.ToString() == "库管员")
{
if (this.textBox1.Text == "2017270140" && this.textBox2.Text == "123456")
{
MessageBox.Show("收银员登录成功");
}
else
{
MessageBox.Show("用户名或密码错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
}
9.退出设置
// 点击“退出”按钮则退出应用程序
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
四.功能完善
1. 输入用户名后回车,光标跳转到密码输入框(涉及到 KeyPress 事件和 Tab 键顺序)
//在用户名输入框中按“回车”,光标跳转到密码输入框
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter)
{
SendKeys.Send("{tab}");
}
}
2. 输入密码后回车,则直接登录(涉及到 TextBox 的 KeyPress 事件)
//在密码输入框中按“回车”,则直接登录
private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter)
{
this.button1_Click(sender, e);
}
}
3. 按 Tab 进入输入框时,自动全选(涉及到 TextBox 的 Enter 事件)
//Tab进入用户名输入框时,自动全选用户名
private void textBox1_Enter(object sender, EventArgs e)
{
((TextBox)sender).SelectAll();
}
//Tab进入密码输入框时,自动全选密码
private void textBox2_Enter(object sender, EventArgs e)
{
((TextBox)sender).SelectAll();
}