1. 程式人生 > 其它 >WinForm(五)控制元件和它的成員

WinForm(五)控制元件和它的成員

  窗體無疑是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();
        }
        #endregion
        private 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中有當前單元格的行與列下標。

  學習控制元件是一個細活,一個一個過,關注他們的屬性,事件,雖然方法比較少,但也是要關注的,就像研究一個類,就要看他的成員都有什麼,各自作用是什麼一樣。

  想要更快更方便的瞭解相關知識,可以關注微信公眾號