使用软件:Visual Studio
创建window窗体:选择window窗口程序,改主窗体名字:Frm开头
窗体常用属性
- 修改标题栏文本:Text
- 修改标题栏图片:Icon (只能是icon后缀名结尾的图片)
-
-
true:最大化--------false:静止最大化
-
-
窗体最小化:MinimizeBox
-
true:可以最小化----------false:禁止最小化
-
-
启动位置:StartPosition
-
屏幕居中:centerScreen
-
-
窗体边框:FormBorderStyle
-
固定边框大小:FixedSing
- None:没有边框
-
-
起始状态:WindowState
-
Maxmized:最大化 --------Minimized:最小化
-
-
背景图片:BackgroundImage
-
背景图片布局:BackgroundImageLayout
-
不平铺:none--------平铺:Title-----------中心:Center----------按图片比例:Zoom--------按窗体大小:Stretch
-
-
快速入门范例:登陆窗体
拖控件,修改名称,用事件:label button text
密码框字符替换:PasswordChar或者UseSystemPasswordChar
效果:
取消按钮单击事件:this.Close(); == 退出此窗口
实现登陆验证:登陆按钮--》单击事件
1 #region 登陆 2 private void Login() 3 { 4 //获取数据 5 String userName = txtLogin.Text.Trim(); 6 String password = txtPassword.Text.Trim(); 7 //有效性验证 8 if (userName.Length == 0) 9 { 10 MessageBox.Show("请输入用户名!","提示",MessageBoxButtons.OK,MessageBoxIcon.Exclamation); 11 return; 12 } 13 if (password.Length == 0) 14 { 15 MessageBox.Show("请输入密码!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); 16 return; 17 } 18 //登陆业务 19 if (userName == "tom" && password == "123456") 20 { 21 MessageBox.Show("登陆成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); 22 //打开窗口 实例化对象 23 FrmMain mian = new FrmMain(); 24 //窗口名 25 mian.Text = String.Format("欢迎你{0}", userName); 26 //打开主窗口 27 mian.Show(); 28 //隐藏当前窗口 29 this.Hide(); 30 } 31 else if (userName == "jom" && password == "123456") 32 { 33 MessageBox.Show("登陆成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); 34 //打开窗口 实例化对象 35 FrmMain main = new FrmMain(); 36 //窗口名 37 main.Text = String.Format("欢迎你{0}", userName);39 //打开主窗口 40 main.Show(); 41 //隐藏当前窗口 42 this.Hide(); 43 } 44 else 45 { 46 MessageBox.Show("用户名或密码错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); 47 } 48 } 49 #endregion 50 51 52 53 //退出 54 private void butClose_Click(object sender, EventArgs e) 55 { 56 this.Close(); 57 } 58 59 //登陆 60 private void butLogin_Click(object sender, EventArgs e) 61 { 62 Login(); 63 }
窗口用到的属性:AcceptButtion关联回车键,打开时 = WindosState窗体最大化 = 所有其他窗体之上TopMost = 背景颜色:BackgroundColor = 窗体边框:FormBorderStyle = 起始状态:WindowState = 禁止窗体最小化:MinimizeBox = 禁止窗体最大化:MaxmizeBox
单击事件...关闭事件 FormClosing;
关闭方法
Application.Exit();