使用軟件: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();