窗體無疑是WinForm的主角,每個窗體都是用一個class來承載,那么窗體的控件,就是類中的私有字段了。每個窗體有三個文件,兩個.cs文件,是一個分部類,Designer.cs是自動生成的C#代碼,一般是拖拽控件后生成的代碼;另一個.cs文件是寫業務代碼用的;第三個是.resx文件,是資源文源,窗體的圖片,圖標,以及一些控件的配置信息。
比如下面的窗體:
生成的代碼如下這么多,可見設計器出力不少。
注:下面代碼只是為了讓你看它有多長,不需要仔細研究。
namespace WinFormDemo04
{partial class Form1{/// <summary>/// Required designer variable./// </summary>private System.ComponentModel.IContainer components = null;/// <summary>/// Clean up any resources being used./// </summary>/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>protected override void Dispose(bool disposing){if (disposing && (components != null)){components.Dispose();}base.Dispose(disposing);}#region Windows Form Designer generated code/// <summary>/// Required method for Designer support - do not modify/// the contents of this method with the code editor./// </summary>private void InitializeComponent(){this.button1 = new System.Windows.Forms.Button();this.textBox1 = new System.Windows.Forms.TextBox();this.comboBox1 = new System.Windows.Forms.ComboBox();this.listBox1 = new System.Windows.Forms.ListBox();this.label1 = new System.Windows.Forms.Label();this.treeView1 = new System.Windows.Forms.TreeView();this.dataGridView1 = new System.Windows.Forms.DataGridView();this.gridId = new System.Windows.Forms.DataGridViewTextBoxColumn();this.gridName = new System.Windows.Forms.DataGridViewTextBoxColumn();this.pictureBox1 = new System.Windows.Forms.PictureBox();this.radioButton1 = new System.Windows.Forms.RadioButton();this.checkBox1 = new System.Windows.Forms.CheckBox();((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();this.SuspendLayout();// // button1// this.button1.Location = new System.Drawing.Point(35, 27);this.button1.Name = "button1";this.button1.Size = new System.Drawing.Size(75, 23);this.button1.TabIndex = 0;this.button1.Text = "button1";this.button1.UseVisualStyleBackColor = true;this.button1.Click += new System.EventHandler(this.button1_Click);// // textBox1// this.textBox1.Location = new System.Drawing.Point(203, 27);this.textBox1.Name = "textBox1";this.textBox1.Size = new System.Drawing.Size(100, 23);this.textBox1.TabIndex = 1;this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);// // comboBox1// this.comboBox1.FormattingEnabled = true;this.comboBox1.Location = new System.Drawing.Point(325, 26);this.comboBox1.Name = "comboBox1";this.comboBox1.Size = new System.Drawing.Size(121, 25);this.comboBox1.TabIndex = 2;this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);// // listBox1// this.listBox1.FormattingEnabled = true;this.listBox1.ItemHeight = 17;this.listBox1.Items.AddRange(new object[] {"111","222","aaa"});this.listBox1.Location = new System.Drawing.Point(199, 84);this.listBox1.Name = "listBox1";this.listBox1.Size = new System.Drawing.Size(120, 89);this.listBox1.TabIndex = 3;this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);// // label1// this.label1.AutoSize = true;this.label1.Location = new System.Drawing.Point(138, 30);this.label1.Name = "label1";this.label1.Size = new System.Drawing.Size(43, 17);this.label1.TabIndex = 4;this.label1.Text = "label1";this.label1.Click += new System.EventHandler(this.label1_Click);// // treeView1// System.Windows.Forms.TreeNode treeNode1 = new System.Windows.Forms.TreeNode("節點1");System.Windows.Forms.TreeNode treeNode2 = new System.Windows.Forms.TreeNode("節點2");System.Windows.Forms.TreeNode treeNode3 = new System.Windows.Forms.TreeNode("節點0", new System.Windows.Forms.TreeNode[] {treeNode1,treeNode2});System.Windows.Forms.TreeNode treeNode4 = new System.Windows.Forms.TreeNode("節點4");System.Windows.Forms.TreeNode treeNode5 = new System.Windows.Forms.TreeNode("節點5");System.Windows.Forms.TreeNode treeNode6 = new System.Windows.Forms.TreeNode("節點3", new System.Windows.Forms.TreeNode[] {treeNode4,treeNode5});this.treeView1.Location = new System.Drawing.Point(325, 84);this.treeView1.Name = "treeView1";treeNode1.Name = "節點1";treeNode1.Text = "節點1";treeNode2.Name = "節點2";treeNode2.Text = "節點2";treeNode3.Name = "節點0";treeNode3.Text = "節點0";treeNode4.Name = "節點4";treeNode4.Text = "節點4";treeNode5.Name = "節點5";treeNode5.Text = "節點5";treeNode6.Name = "節點3";treeNode6.Text = "節點3";this.treeView1.Nodes.AddRange(new System.Windows.Forms.TreeNode[] {treeNode3,treeNode6});this.treeView1.Size = new System.Drawing.Size(121, 97);this.treeView1.TabIndex = 5;this.treeView1.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterSelect);// // dataGridView1// this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {this.gridId,this.gridName});this.dataGridView1.Location = new System.Drawing.Point(30, 209);this.dataGridView1.Name = "dataGridView1";this.dataGridView1.Size = new System.Drawing.Size(258, 150);this.dataGridView1.TabIndex = 6;this.dataGridView1.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellContentClick);// // ID// this.gridId.HeaderText = "ID";this.gridId.Name = "ID";// // GridName// this.gridName.HeaderText = "Name";this.gridName.Name = "GridName";// // pictureBox1// this.pictureBox1.Location = new System.Drawing.Point(321, 221);this.pictureBox1.Name = "pictureBox1";this.pictureBox1.Size = new System.Drawing.Size(100, 50);this.pictureBox1.TabIndex = 7;this.pictureBox1.TabStop = false;this.pictureBox1.Click += new System.EventHandler(this.pictureBox1_Click);// // radioButton1// this.radioButton1.AutoSize = true;this.radioButton1.Location = new System.Drawing.Point(33, 89);this.radioButton1.Name = "radioButton1";this.radioButton1.Size = new System.Drawing.Size(102, 21);this.radioButton1.TabIndex = 8;this.radioButton1.TabStop = true;this.radioButton1.Text = "radioButton1";this.radioButton1.UseVisualStyleBackColor = true;this.radioButton1.CheckedChanged += new System.EventHandler(this.radioButton1_CheckedChanged);// // checkBox1// this.checkBox1.AutoSize = true;this.checkBox1.Location = new System.Drawing.Point(33, 125);this.checkBox1.Name = "checkBox1";this.checkBox1.Size = new System.Drawing.Size(89, 21);this.checkBox1.TabIndex = 9;this.checkBox1.Text = "checkBox1";this.checkBox1.UseVisualStyleBackColor = true;this.checkBox1.CheckedChanged += new System.EventHandler(this.checkBox1_CheckedChanged);// // Form1// this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 17F);this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;this.ClientSize = new System.Drawing.Size(497, 418);this.Controls.Add(this.checkBox1);this.Controls.Add(this.radioButton1);this.Controls.Add(this.pictureBox1);this.Controls.Add(this.dataGridView1);this.Controls.Add(this.treeView1);this.Controls.Add(this.label1);this.Controls.Add(this.listBox1);this.Controls.Add(this.comboBox1);this.Controls.Add(this.textBox1);this.Controls.Add(this.button1);this.Margin = new System.Windows.Forms.Padding(2);this.Name = "Form1";this.Text = "Form1";this.Load += new System.EventHandler(this.Form1_Load);((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();this.ResumeLayout(false);this.PerformLayout();}#endregionprivate Button button1;private TextBox textBox1;private ComboBox comboBox1;private ListBox listBox1;private Label label1;private TreeView treeView1;private DataGridView dataGridView1;private PictureBox pictureBox1;private RadioButton radioButton1;private CheckBox checkBox1;private DataGridViewTextBoxColumn gridId;private DataGridViewTextBoxColumn gridName;}
}
一個class中的成員可以是字段,屬性,方法,構造函數,析構函數,事件,索引器,重載運算符,常量,內部嵌套類。通常,一個類庫中的class,屬性和方法是最常見的,到可視化窗體或控件類時,屬性和事件是最常見的,這是因為通過電腦輸入設備或內部的動作,會觸發很多事件,這些事件會調用我們訂閱的方法,從而完成業務的流轉。所以學習可視化控件,就是學習他的屬性和事件。每個控件,雙擊后自動訂閱的事件就是這個控件最常用的控件,比如Button的Click,TextBox的TextChanged事件等。
有一些控件還有子控件或子選項,就像一個類型的一個屬性是集合類型一些,可以添加子類型。有一些控件是容器控件,用來承載其他控件的,它有一個Controles的屬性,可以添加其他類型的控件。
關于每個控件的使用這里就不展開了,因為相關的資料很多,官方的文檔就是不錯的選擇。
在WinForm中,大部分事件都是以xxxEventHandler作為定義事件的委托,并且有兩個參數,第一個是object sender,就是發出事件的控件,第二個參數是EventArgs或它的子類,如果是它子類,會攜帶一些事件的參數。xxxEventHandler和xxxEventArgs與EventHandler和EventArgs是委托和參數的關系,也是對應的。
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
比如DataGridViewCellEventHandler和DataGridViewCellEventArgs,并且Args中有當前單元格的行與列下標。
學習控件是一個細活,一個一個過,關注他們的屬性,事件,雖然方法比較少,但也是要關注的,就像研究一個類,就要看他的成員都有什么,各自作用是什么一樣。