
1 namespace firstly #當前命名空間控件 2 { 3 public partial class Form1 : Form 4 { 5 public Form1() #第一個窗體 6 { 7 InitializeComponent(); 8 } 9 } 10 }
1.常用控件的認識
修改winform 應用程序的入口點

using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; //程序位於 Program.cs namespace firstly { static class Program { /// <summary> /// 應用程序的主入口點。 /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); //決定那個窗體先被執行 Application.Run(new Form22()); // Application.Run(new Form1()); } } }
可以通過修改 Form1.Designer.cs 來實現 對控件進行修改 (一般 圖形進行控制)
button1.Enabled = false; 是指不可用
Application.Exit(); 關閉總窗體
Form.Close(); 關閉當前的窗體
(1).基本控件
1.Label 標簽
功能 :顯示窗體文本
常用屬性和方法 事件 :Text
Hide 即是Visible 屬性設置為True 控件也不可見
Show 相當於Visible屬性設置為True
Click: 單擊控件
eg:
在一個界面 ,在此打開一個新的窗體,(當前窗體隱藏)

1 //當前界面隱藏 2 linkLabel1.LinkVisited = true; //確認可以被訪問 3 Form2 f = new Form2(); 4 f1.Show(); 5 this.Hide(); // 當前隱藏
2.TextBox 文本框的控件和 Button 按鈕
功能 接受或顯示用戶文本信息
常用屬性和方法 事件 :
MaxLength 文本框輸入最大字符
Multiline 是否可以在文本框輸入多行
Passwordchar 密碼輸入框
Readonly 文本框中的字體為可讀
Text 檢索在控件輸入的文本
Clear 刪除現有的所有文本
Show 相當於Visible屬性設置為True
KeyPress 用戶按一個鍵結束時將發生該事件
Botton控件
Enabled 是否啟用
PerFormClick Button中Click時間
Click 點擊觸發的
eg 用戶登錄

1 private void button2_Click(object sender, EventArgs e) //取消 2 { 3 clear(); //調用自己構造的方法 4 5 } 6 7 private void button1_Click(object sender, EventArgs e) 8 { 9 if (textBox1.Text == string.Empty || textBox2.Text == string.Empty) 10 { 11 MessageBox.Show("信息禁止為空!","登錄提示"); 12 clear(); 13 return; 14 } 15 if (!textBox1.Text.Equals("admin") || !textBox2.Text.Equals("admin")) # 輸入比對 16 { 17 MessageBox.Show("用戶名稱或密碼為空!", "登錄提示"); 18 clear(); 19 return; 20 } 21 else 22 { 23 MessageBox.Show("歡迎您登錄本系統!","消息提示"); 24 clear(); 25 } 26 } 27 //由於每次都調用 所以生成一個方法 更有效率 28 public void clear() 29 { 30 textBox1.Clear(); 31 textBox2.Clear(); 32 textBox2.Focus(); 33 }

1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Text; 7 using System.Windows.Forms; 8 9 namespace WindowsApplication1 10 { 11 public partial class Form1 : Form 12 { 13 public Form1() 14 { 15 InitializeComponent(); 16 } 17 private bool ISLOGIN = false; 18 //就加在其構造函數的前面。這東西我們等下要用。 19 //然后加一個屬性,該屬性用來判別是否登錄 20 public bool isLogin 21 { 22 get 23 { 24 return this.ISLOGIN; 25 } 26 } 27 private void Form1_Load(object sender, EventArgs e) 28 { 29 30 } 31 32 private void button1_Click(object sender, EventArgs e) 33 { 34 if (this.textBox1.Text == "qq" && this.textBox2.Text == "qq") 35 { 36 this.ISLOGIN = true; 37 //frmEditor f2 = new frmEditor(); 38 //f2.ShowDialog(); 39 this.Close();//登陸成功才關閉登陸登陸窗體 40 } 41 else 42 { 43 MessageBox.Show("非法用戶名或密碼,請再重試一次!"); 44 } 45 } 46 } 47 }
Form1.MaximizeBox=false 窗體不允許最大化
text1.PasswordChar='*';
3.ListBox 列表框 控件
功能顯示多行文本信息 , 提供用戶選擇
常用屬性和方法 事件 :
Items : 列表框的具體內容
SelectionMode 列表框類型 單選 ,多選, 不可選擇
SelectedIndex 指定選中行索引,默認第一行為0
SelectedItem 被選擇的文本內容
SelectedItems ListBox的選擇列表集合
Text 默認文本內容
ClearSelected 清除當前選擇
SelectedIndexChanged 一旦改變就觸發
eg。部門顯示

private void Form1_Load(object sender, EventArgs e) //窗體加載 { this.listBox1.Items.Add("軟件部"); this.listBox1.Items.Add("硬件部"); this.listBox1.Items.Add("財務部"); this.listBox1.Items.Add("人事部"); } //當選中后會觸發的事件 private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { MessageBox.Show("您選擇的部門是:"+listBox1.SelectedItem.ToString()+",位列第"+listBox1.SelectedIndex.ToString(),"信息提示"); }

1 private void button1_Click_1(object sender, EventArgs e) 2 { 3 textBox1.Enabled = true; 4 textBox2.Enabled = true; 5 comboBox1.Enabled = true; 6 listBox1.Enabled = true; 7 textBox1.Focus(); 8 } 9 10 private void button2_Click(object sender, EventArgs e) 11 { 12 textBox1.Text = ""; 13 textBox2.Text = ""; 14 listBox1.SelectedIndex = 0; 15 comboBox1.SelectedIndex = 0; 16 textBox1.Focus(); 17 } 18 19 private void button3_Click(object sender, EventArgs e) 20 { 21 string str = ""; 22 for (int ctr = 0; ctr <= this.listBox1.SelectedItems.Count - 1; ctr++) 23 { 24 str += "\n" + this.listBox1.SelectedItems[ctr].ToString(); 25 } 26 //注意:此處需要將listBox1的selectionmodel設置為MultiExtended才會有效果。 27 MessageBox.Show("選定的項目是:\n" + str, "信息提示", MessageBoxButtons.OK); 28 Application.Exit(); 29 } 30 31 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 32 { 33 string str = this.comboBox1.SelectedItem.ToString(); 34 MessageBox.Show("您選擇的職務是:\n" + str, "信息提示", MessageBoxButtons.OK); 35 }
4.ComboBox
功能 限制用戶 在多個固定信息選擇 唯一一個行
常用屬性和方法 事件 :
DropDownStyle : ComboBox 控件的樣式 有三種 simple 只讀 顯示所有 DropDown(可以讀寫) DropDownList(只讀)
MaxDropDownItems 下拉區顯示最大項目數
Select 選擇指定范圍的文本

1 private void Form1_Load(object sender, EventArgs e) 2 { 3 this.comboBox1.Items.Add("財務部"); 4 this.comboBox1.Items.Add("產品部"); 5 this.comboBox1.Items.Add("銷售部"); 6 this.comboBox1.Items.Add("生產部"); 7 //默認的選擇是"產品部" 8 this.comboBox1.SelectedIndex = 1; 9 10 this.comboBox2.Items.Add("財務部"); 11 this.comboBox2.Items.Add("產品部"); 12 this.comboBox2.Items.Add("銷售部"); 13 this.comboBox2.Items.Add("生產部"); 14 //默認的選擇是"產品部" 15 this.comboBox2.SelectedIndex = 1; 16 17 this.comboBox3.Items.Add("財務部"); 18 this.comboBox3.Items.Add("產品部"); 19 this.comboBox3.Items.Add("銷售部"); 20 this.comboBox3.Items.Add("生產部"); 21 //默認的選擇是"產品部" 22 this.comboBox3.SelectedIndex = 1; 23 } 24 25 private void com_selectIndexChanged_1(){ 26 String mess=comboBox1.SelectedItem.ToString(); 27 TextBox1.text=mess; 28 29 }
5.對話框窗口(非模式窗體)
重載方法 |
Show(string text); |
Show(string text, string caption); |
Show(string text, string caption, MessageBoxButtons buttons); |
Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon); 指定文本/標題/按鈕/圖標的消息框 |
|

1 private void button1_Click(object sender, EventArgs e) 2 { 3 MessageBox.Show("嘿,這是簡單提示!","信息提示"); 4 } 5 6 private void button2_Click(object sender, EventArgs e) 7 { 8 DialogResult result = MessageBox.Show("嘿,這是問詢提示!","問詢提示",MessageBoxButtons.YesNo); 9 if (result == DialogResult.Yes) 10 { 11 label1.Text = "您選擇了YES"; 12 13 } 14 else 15 { 16 label1.Text = "您選擇了NO"; 17 } 18 } 19 20 21 22 private void button3_Click(object sender, EventArgs e) 23 { 24 DialogResult result = MessageBox.Show("嘿,這是帶有圖標的問詢提示!", "問詢提示", MessageBoxButtons.YesNoCancel,MessageBoxIcon.Question,MessageBoxDefaultButton.Button3,MessageBoxOptions.RightAlign); 25 if (result == DialogResult.Yes) 26 { 27 label1.Text = "您選擇了圖標YES"; 28 29 } 30 else if(result==DialogResult.Cancel) 31 { 32 label1.Text = "您選擇了圖標取消"; 33 } 34 else if (result == DialogResult.No) 35 { 36 label1.Text = "您選擇了圖標NO"; 37 } 38 }
補充 showDialog
路徑綁定show 不可以自由切換 就是指在用戶沒有關閉當前頁的前提下,無法關閉該頁面后任一頁面
要等待當前窗體關閉后才能操作其他窗體,
eg

1 private void button2_Click(object sender, EventArgs e) 2 { 3 Form2 f2 = new Form2(); 4 this.Visible = false; 5 f2.ShowDialog(); 6 this.Visible = true; 7 }

1 private void button1_Click_1(object sender, EventArgs e) 2 { 3 textBox1.Enabled = true; 4 textBox2.Enabled = true; 5 comboBox1.Enabled = true; 6 listBox1.Enabled = true; 7 textBox1.Focus(); 8 } 9 10 private void button2_Click(object sender, EventArgs e) 11 { 12 textBox1.Text = ""; 13 textBox2.Text = ""; 14 listBox1.SelectedIndex = 0; 15 comboBox1.SelectedIndex = 0; 16 textBox1.Focus(); 17 } 18 19 private void button3_Click(object sender, EventArgs e) 20 { 21 string str = ""; 22 for (int ctr = 0; ctr <= this.listBox1.SelectedItems.Count - 1; ctr++) 23 { 24 str += "\n" + this.listBox1.SelectedItems[ctr].ToString(); 25 } 26 //注意:此處需要將listBox1的selectionmodel設置為MultiExtended才會有效果。 27 MessageBox.Show("選定的項目是:\n" + str, "信息提示", MessageBoxButtons.OK); 28 Application.Exit(); 29 } 30 31 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 32 { 33 string str = this.comboBox1.SelectedItem.ToString(); 34 MessageBox.Show("您選擇的職務是:\n" + str, "信息提示", MessageBoxButtons.OK); 35 }
2.多文檔處理界面
SDI 單文檔窗口
MDI 多文檔窗口
設置MDI *****在窗體 屬性IsMdiContainer 屬性設置為True
MDI 屬性 |
StartPosition 初始窗口位置 |
CancelButton 按下esc鍵后執行那個按鈕。 |
ControlBox 確定系統是否有圖標和最大最小關閉按鈕。 |
FormBorderStyle 指定邊框和標題欄的外觀和行為。 |
HelpButton 確定窗體的標題欄上是否有幫助按鈕。 |
KeyPreview 確定窗體鍵盤事件是否已經向窗體注冊。 |
MainMenuStrip 確定鍵盤激活和多文檔合並。 |
ShowInTaskbar 確定窗體是否出現在任務欄中。 |
WindowState 確定窗體的初始可視狀態。 |
方法 |
Activate 當窗體被激活時候發生 |
MdiChildActivate 當MDI子窗體被激活時候發生 |
事件 |
Activated 窗體在激活時發生 |
Closed 已經關閉 |
Closing 正在關閉 |
Load |
為MDI 配置 MainMenu菜單 工具欄中默認是沒有的,打開工具箱 界面,右鍵 選擇項命令
並在擴展的選擇工具箱項窗口 尋找 Mainmenu 控件
父窗體設置
IsMdiContainer 屬性設置為True
觸發方法:
this.LayoutMdi(Mdilayout.Cascade) ;
Form2 f2 = new Form2();
指定父窗體
f2.MdiParent = this;
f2.Show();
在此可以在一個界面 內打開一個子窗體
窗體的層疊
TileHorizontal 水平
TileVertical 垂直
小技巧:
*****窗體之間傳值
實現界面
窗體2
窗體3 接受到

1 ============form2的設計===================== 2 private void Form2_Load(object sender, EventArgs e) 3 { 4 comboBox1.SelectedIndex = 0; 5 textBox3.Text = ""; 6 textBox1.Focus(); 7 } 8 9 public void button1_Click(object sender, EventArgs e) 10 { 11 if (textBox1.Text == "" || textBox2.Text == "" || comboBox1.Text == "") 12 { 13 MessageBox.Show("姓名,或者郵件,或者提交,信息禁止為空!", "信息提示"); 14 } 15 else 16 { 17 this.Hide(); 18 Form3 childform3 = new Form3(this.textBox1.Text,this.textBox2.Text,this.comboBox1.SelectedItem.ToString(),this.textBox3.Text); 19 childform3.Show(); 20 } 21 } 22 private void button2_Click(object sender, EventArgs e) 23 { 24 this.Close(); 25 } 26 27 28 ================form3的設計==================== 29 對於form3窗體而言,在系統初始事件填寫如下代碼: 30 public partial class Form3 : Form 31 { 32 private string _name; 33 private string _emailId; 34 private string _subject; 35 private string _feedBack; 36 37 public Form3(string varName, string varEmail, string varSubject, string varFeedBack) 38 { 39 InitializeComponent(); 40 // 在 private 變量中存儲值 41 this._name = varName; 42 this._emailId = varEmail; 43 this._subject = varSubject; 44 this._feedBack = varFeedBack; 45 // 在列表框中放置值 46 listBox1.Items.Add("姓名:" + this._name); 47 listBox1.Items.Add("郵件地址:" + this._emailId); 48 listBox1.Items.Add("信息主題:" + this._subject); 49 listBox1.Items.Add("反饋意見:" + this._feedBack); 50 } 51 52 private void button1_Click(object sender, EventArgs e) 53 { 54 MessageBox.Show("感謝您輸入的反饋!"); 55 this.Close(); 56 } 57 }
上例子中固然很好 ,但是 2個窗體在轉換過程中沒有收到MDI 的控制

1 ============form2的設計===================== 2 private void Form2_Load(object sender, EventArgs e) 3 { 4 comboBox1.SelectedIndex = 0; 5 textBox3.Text = ""; 6 textBox1.Focus(); 7 } 8 9 public void button1_Click(object sender, EventArgs e) 10 { 11 if (textBox1.Text == "" || textBox2.Text == "" || comboBox1.Text == "") 12 { 13 MessageBox.Show("姓名,或者郵件,或者提交,信息禁止為空!", "信息提示"); 14 } 15 else 16 { 17 this.Hide(); 18 Form3 childform3 = new Form3(this.textBox1.Text,this.textBox2.Text,this.comboBox1.SelectedItem.ToString(),this.textBox3.Text); 19 //加的這一句 20 childform3.MdiParent = this; 21 22 childform3.Show(); 23 } 24 } 25 private void button2_Click(object sender, EventArgs e) 26 { 27 this.Close(); 28 } 29 30 31 ================form3的設計 32 在 中加入返回按鈕事件 =================== 33 34 Form2 f2 = new Form2(); 35 f2.MdiParent = this.MdiParent; 36 f2.Show(); 37 this.close();
技巧2 放回重復打開窗口
在打開的同時進行判斷

1 建議寫成一個方法 2 直接檢測是否已經打開此MDI窗體 3 // 是否已經打開了?(用循環來判斷) 4 foreach (Form childrenForm in this.MdiChildren) { 5 //檢測是不是當前子窗體名稱 6 if (childrenForm.Name == "子窗體名稱") 7 { //是的話就是把他顯示 8 childrenForm.Visible = true; 9 //並激活該窗體 10 childrenForm.Activate(); 11 return; 12 } 13 } 14 //下面是打開子窗體 15 Form1 childrenForm = new Form1(); 16 childrenForm.MdiParent = this; 17 childrenForm.Show(); 18 }
技巧3 通過類屬性進行數據傳值(方法一)
1 1、先在Form2中定義一個成員變量和一個屬性如下: 2 private string form2zhi = null; 3 public string Form2ChuanZhi 4 { get 5 { return form2zhi; } 6 } 7 2、再在Form3中定義一個成員變量和一個屬性如下: 8 private string form3zhi = null; 9 public string Form3ChuanZhi 10 { set { form3zhi = value; 11 } 12 get 13 { return form3zhi; 14 } 15 } 16 17 3、雙擊btn_ChuanZhi在這個事件中寫入以下代碼(主要是顯示Form3窗體和將Form2中的值傳過去): 18 Form3 form3 = new Form3(); 19 form3.Form3ChuanZhi = form2zhi; 20 //將值傳過去 21 form3.Show(); 22 24 另一個版本 25 private string some_name; 26 public string SomeName { 27 get { 28 return some_name; 29 } 30 set { 31 some_name = value; 32 } 33 } 34 private void button2_Click(object sender, EventArgs e) 35 { 36 Form2 f2 = new Form2(); 37 this.Visible = false; 38 f2.ShowDialog(); 39 this.Visible = true; 40 } 41 private void menuItem3_Click(object sender, EventArgs e) 42 { 43 if (TextBox1.text1 == "" || TextBox2.Text == "") { 44 MessageBox.Show("no blank"); 45 }else{ 46 this.Hide(); 47 Form2 fm=new Form2(); 48 fm.SomeName=text1Box1.Text; 49 fm.MdiParent=this.MdiParent; 50 fm.Show(); 51 } 52 private void Form1_Load(object sender, EventArgs e) 53 { 54 listBox1.Items.Add(SomeName); 55 }
實訓:

1 Form 4 2 3 ———————————————————— 4 using System; 5 using System.Collections.Generic; 6 using System.ComponentModel; 7 using System.Data; 8 using System.Drawing; 9 using System.Text; 10 using System.Windows.Forms; 11 12 using System.Collections; 13 //注意:必須調用該命名空間,否則無法建立ArrayList對象。 14 namespace WindowsApplication1 15 { 16 public partial class Form4 : Form 17 { 18 //Form4窗體的私有變量listData1,用來保存listBox1下拉列表中的數據 19 private ArrayList listData1; 20 //Form4窗體的公共屬性。該屬性可以被外部所訪問,訪問的內容就是Form4窗體的私有變量listData1的內容。 21 //這里我們采用屬性,感覺語法更靈活,清楚。 22 public ArrayList ListData2 23 { 24 get { return this.listData1; } 25 } 26 27 //配置Form4構造函數,目的是填充listBox1中的具體數據內容 28 public Form4() 29 { 30 InitializeComponent(); 31 this.listData1 = new ArrayList(); 32 this.listData1.Add("DotNet"); 33 this.listData1.Add("C#"); 34 this.listData1.Add("Asp.net"); 35 this.listData1.Add("WebService"); 36 this.listData1.Add("XML"); 37 this.listBox1.DataSource = this.listData1; 38 } 39 40 private void button1_Click(object sender, EventArgs e) 41 { 42 Form5 formChild = new Form5();//實例化另外一個窗體Form5 43 formChild.Owner = this; 44 //我們設置了formChild.Owner為this,這樣,子窗體和主窗體就有聯系了, 45 formChild.ShowDialog();//打開子窗體 46 //當然上面兩句也可以該成為:formChild.ShowDialog(this); 47 this.listBox1.DataSource = null; 48 this.listBox1.DataSource = this.listData1; 49 this.Hide(); 50 this.Close(); 51 } 52 } 53 } 54 55 ————————————Form 5 56 57 using System; 58 using System.Collections.Generic; 59 using System.ComponentModel; 60 using System.Data; 61 using System.Drawing; 62 using System.Text; 63 using System.Windows.Forms; 64 using System.Collections; 65 66 namespace WindowsApplication1 67 { 68 public partial class Form5 : Form 69 { 70 private ArrayList listData3; 71 //建立私有變量,該變量用以接收復窗體的共有屬性數據 72 public Form5() 73 { 74 InitializeComponent(); 75 //千萬別放到構造函數里面,因為在主窗體修改按鈕被點擊后,開始執行Form5 formChild = new Form5(); 76 //而在Form2的實例化過程中會在構造函數中執行Form4 pareForm = (Form4)this.Owner;而這時的this.Owner是沒有值的,為空引用, 77 } 78 79 private void Form5_Load(object sender, EventArgs e) 80 { 81 Form4 pareForm = (Form4)this.Owner;//實例化父窗體,建立父子窗體之間聯系 82 this.listData3 = pareForm.ListData2;//訪問父窗體的公有屬性給當前子窗體的私有變量 83 foreach (object o in this.listData3)//遍歷后將數據顯示在listBox1之中 84 { 85 this.listBox1.Items.Add(o); 86 } 87 } 88 89 private void button1_Click(object sender, EventArgs e) 90 { 91 Application.Exit(); 92 } 93 } 94 }
3 菜單和菜單組件
分類 菜單欄 主菜單和子菜單
MenuStrip
方法
text 文件(&N) 會產生 文件(N) 運行時Alt+N 執行
ShortcutKeys 自定義快捷鍵組合 ,注:至少選擇一項修飾符 Alt ,shift,ctrl
Separator 分割線
美化: *.skn *.ssk
第三方動態鏈接庫