WinForm(五)控件和它的成员

时间:2022-12-11 18:58:54

  窗体无疑是WinForm的主角,每个窗体都是用一个class来承载,那么窗体的控件,就是类中的私有字段了。每个窗体有三个文件,两个.cs文件,是一个分部类,Designer.cs是自动生成的C#代码,一般是拖拽控件后生成的代码;另一个.cs文件是写业务代码用的;第三个是.resx文件,是资源文源,窗体的图片,图标,以及一些控件的配置信息。

  比如下面的窗体:

WinForm(五)控件和它的成员

  生成的代码如下这么多,可见设计器出力不少。

  注:下面代码只是为了让你看它有多长,不需要仔细研究。

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中有当前单元格的行与列下标。

  学习控件是一个细活,一个一个过,关注他们的属性,事件,虽然方法比较少,但也是要关注的,就像研究一个类,就要看他的成员都有什么,各自作用是什么一样。

  想要更快更方便的了解相关知识,可以关注微信公众号

WinForm(五)控件和它的成员