简单的Windows登陆界面设计

来源:互联网 发布:c 语言 多线程共享数据 编辑:程序博客网 时间:2024/05/21 17:39
要求:
1.用户名必须为字母。
 1 //限定用户名必须为字母 2         private void txtName_KeyPress(object sender, KeyPressEventArgs e) 3         { 4             if ((e.KeyChar >= 'a' && e.KeyChar <= 'z') || (e.KeyChar >= 'A' && e.KeyChar <= 'Z')) 5             { 6                 e.Handled = false; 7             } 8             else { 9                 MessageBox.Show("用户名只能为字母!");10                 e.Handled = true;11             }12         }

 

2.光标进入文本框时背景蓝色,文字白色;光标离开文本框时,背景白色,文字黑色。
界面:
 1   //光标进入文本框时,背景为蓝色,字体为白色; 2         //光标离开文本框时,背景为白色,字体为黑色。 3         private void txtName_Enter(object sender, EventArgs e) 4         { 5             txtName.ForeColor = Color.White; 6             txtName.BackColor = Color.Blue; 7         } 8  9         private void txtName_Leave(object sender, EventArgs e)10         {11             txtName.BackColor = Color.White;12             txtName.ForeColor = Color.Black;13         }

 

3.当输入用户名“admin”和密码“123”之后,单击”确定“按钮,系统将弹出消息框以显示输入正确,否则显示用户名或密码错误的提示信息。

 1 private void btnLogin_Click(object sender, EventArgs e) 2         { 3             string userName = txtName.Text; 4             string password = txtPwd.Text; 5             if (userName == "admin" && password == "123") 6             { 7                 MessageBox.Show("欢迎进入个人理帐系统!", "登陆成功!", MessageBoxButtons.OK, MessageBoxIcon.Information); 8             } 9             else10             {11                 MessageBox.Show("您输入的用户名或密码错误!", "登录失败!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);12             }13         }

4.单击”取消“按钮,清除输入信息,并将光标定位在txtName文本框中。

 

1   private void btnCancel_Click(object sender, EventArgs e)2         {3             txtName.Text = "";4             txtPwd.Text = "";5             txtName.Focus();6         }

 

5.最终界面: 
 
小技巧:为label设置Image属性,为了让图片完整显示出来,需要把label的AutoSize属性设置为false,然后适当拉大label大小。还要注意,ImageAlign属性设置为MiddleLeft,TextAlign属性设置为MiddleRight。
 

Notice:

(1)ico:是Windows的图标文件格式的一种,可以存储单个图案、多尺寸、多色板的图标文件。
(2)MessageBox:消息框,显示一个模态对话框,其中包含一个系统图标、 一组按钮和一个简短的特定于应用程序消息,如状态或错误的信息。
(3)Button的快捷键通过设置Text属性为”取消(&C)“实现。
(4)此练习使用的软件为Visual Studio 2012,图形资源由VS提供,据说在VS的安装文件夹Common7\ImageLibrary中能找到,没有的话,可以到官网下载。

 

0 0
原创粉丝点击