窗体无疑是WinForm的主角,每个窗体都是用一个class来承载,那么窗体的控件,就是类中的私有字段了。每个窗体有三个文件,两个。cs文件,是一个分部类,Designer。cs是自动生成的C代码,一般是拖拽控件后生成的代码;另一个。cs文件是写业务代码用的;第三个是。resx文件,是资源文源,窗体的图片,图标,以及一些控件的配置信息。 比如下面的窗体: 生成的代码如下这么多,可见设计器出力不少。 注:下面代码只是为了让你看它有多长,不需要仔细研究。namespaceWinFormDemo04{partialclassForm1{summaryRequireddesignervariable。summaryprivateSystem。ComponentModel。IContainercomponentsnull;summaryCleanupanyresourcesbeingused。summaryparamnamedisposingtrueifmanagedresourcesshouldbedisposed;otherwise,false。paramprotectedoverridevoidDispose(booldisposing){if(disposing(components!null)){components。Dispose();}base。Dispose(disposing);}regionWindowsFormDesignergeneratedcodesummaryRequiredmethodforDesignersupportdonotmodifythecontentsofthismethodwiththecodeeditor。summaryprivatevoidInitializeComponent(){this。button1newSystem。Windows。Forms。Button();this。textBox1newSystem。Windows。Forms。TextBox();this。comboBox1newSystem。Windows。Forms。ComboBox();this。listBox1newSystem。Windows。Forms。ListBox();this。label1newSystem。Windows。Forms。Label();this。treeView1newSystem。Windows。Forms。TreeView();this。dataGridView1newSystem。Windows。Forms。DataGridView();this。gridIdnewSystem。Windows。Forms。DataGridViewTextBoxColumn();this。gridNamenewSystem。Windows。Forms。DataGridViewTextBoxColumn();this。pictureBox1newSystem。Windows。Forms。PictureBox();this。radioButton1newSystem。Windows。Forms。RadioButton();this。checkBox1newSystem。Windows。Forms。CheckBox();((System。ComponentModel。ISupportInitialize)(this。dataGridView1))。BeginInit();((System。ComponentModel。ISupportInitialize)(this。pictureBox1))。BeginInit();this。SuspendLayout();button1this。button1。LocationnewSystem。Drawing。Point(35,27);this。button1。Namebutton1;this。button1。SizenewSystem。Drawing。Size(75,23);this。button1。TabIndex0;this。button1。Textbutton1;this。button1。UseVisualStyleBackColortrue;this。button1。ClicknewSystem。EventHandler(this。button1Click);textBox1this。textBox1。LocationnewSystem。Drawing。Point(203,27);this。textBox1。NametextBox1;this。textBox1。SizenewSystem。Drawing。Size(100,23);this。textBox1。TabIndex1;this。textBox1。TextChangednewSystem。EventHandler(this。textBox1TextChanged);comboBox1this。comboBox1。FormattingEnabledtrue;this。comboBox1。LocationnewSystem。Drawing。Point(325,26);this。comboBox1。NamecomboBox1;this。comboBox1。SizenewSystem。Drawing。Size(121,25);this。comboBox1。TabIndex2;this。comboBox1。SelectedIndexChangednewSystem。EventHandler(this。comboBox1SelectedIndexChanged);listBox1this。listBox1。FormattingEnabledtrue;this。listBox1。ItemHeight17;this。listBox1。Items。AddRange(newobject〔〕{111,222,aaa});this。listBox1。LocationnewSystem。Drawing。Point(199,84);this。listBox1。NamelistBox1;this。listBox1。SizenewSystem。Drawing。Size(120,89);this。listBox1。TabIndex3;this。listBox1。SelectedIndexChangednewSystem。EventHandler(this。listBox1SelectedIndexChanged);label1this。label1。AutoSizetrue;this。label1。LocationnewSystem。Drawing。Point(138,30);this。label1。Namelabel1;this。label1。SizenewSystem。Drawing。Size(43,17);this。label1。TabIndex4;this。label1。Textlabel1;this。label1。ClicknewSystem。EventHandler(this。label1Click);treeView1System。Windows。Forms。TreeNodetreeNode1newSystem。Windows。Forms。TreeNode(节点1);System。Windows。Forms。TreeNodetreeNode2newSystem。Windows。Forms。TreeNode(节点2);System。Windows。Forms。TreeNodetreeNode3newSystem。Windows。Forms。TreeNode(节点0,newSystem。Windows。Forms。TreeNode〔〕{treeNode1,treeNode2});System。Windows。Forms。TreeNodetreeNode4newSystem。Windows。Forms。TreeNode(节点4);System。Windows。Forms。TreeNodetreeNode5newSystem。Windows。Forms。TreeNode(节点5);System。Windows。Forms。TreeNodetreeNode6newSystem。Windows。Forms。TreeNode(节点3,newSystem。Windows。Forms。TreeNode〔〕{treeNode4,treeNode5});this。treeView1。LocationnewSystem。Drawing。Point(325,84);this。treeView1。NametreeView1;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(newSystem。Windows。Forms。TreeNode〔〕{treeNode3,treeNode6});this。treeView1。SizenewSystem。Drawing。Size(121,97);this。treeView1。TabIndex5;this。treeView1。AfterSelectnewSystem。Windows。Forms。TreeViewEventHandler(this。treeView1AfterSelect);dataGridView1this。dataGridView1。ColumnHeadersHeightSizeModeSystem。Windows。Forms。DataGridViewColumnHeadersHeightSizeMode。AutoSize;this。dataGridView1。Columns。AddRange(newSystem。Windows。Forms。DataGridViewColumn〔〕{this。gridId,this。gridName});this。dataGridView1。LocationnewSystem。Drawing。Point(30,209);this。dataGridView1。NamedataGridView1;this。dataGridView1。SizenewSystem。Drawing。Size(258,150);this。dataGridView1。TabIndex6;this。dataGridView1。CellContentClicknewSystem。Windows。Forms。DataGridViewCellEventHandler(this。dataGridView1CellContentClick);IDthis。gridId。HeaderTextID;this。gridId。NameID;GridNamethis。gridName。HeaderTextName;this。gridName。NameGridName;pictureBox1this。pictureBox1。LocationnewSystem。Drawing。Point(321,221);this。pictureBox1。NamepictureBox1;this。pictureBox1。SizenewSystem。Drawing。Size(100,50);this。pictureBox1。TabIndex7;this。pictureBox1。TabStopfalse;this。pictureBox1。ClicknewSystem。EventHandler(this。pictureBox1Click);radioButton1this。radioButton1。AutoSizetrue;this。radioButton1。LocationnewSystem。Drawing。Point(33,89);this。radioButton1。NameradioButton1;this。radioButton1。SizenewSystem。Drawing。Size(102,21);this。radioButton1。TabIndex8;this。radioButton1。TabStoptrue;this。radioButton1。TextradioButton1;this。radioButton1。UseVisualStyleBackColortrue;this。radioButton1。CheckedChangednewSystem。EventHandler(this。radioButton1CheckedChanged);checkBox1this。checkBox1。AutoSizetrue;this。checkBox1。LocationnewSystem。Drawing。Point(33,125);this。checkBox1。NamecheckBox1;this。checkBox1。SizenewSystem。Drawing。Size(89,21);this。checkBox1。TabIndex9;this。checkBox1。TextcheckBox1;this。checkBox1。UseVisualStyleBackColortrue;this。checkBox1。CheckedChangednewSystem。EventHandler(this。checkBox1CheckedChanged);Form1this。AutoScaleDimensionsnewSystem。Drawing。SizeF(7F,17F);this。AutoScaleModeSystem。Windows。Forms。AutoScaleMode。Font;this。ClientSizenewSystem。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。MarginnewSystem。Windows。Forms。Padding(2);this。NameForm1;this。TextForm1;this。LoadnewSystem。EventHandler(this。Form1Load);((System。ComponentModel。ISupportInitialize)(this。dataGridView1))。EndInit();((System。ComponentModel。ISupportInitialize)(this。pictureBox1))。EndInit();this。ResumeLayout(false);this。PerformLayout();}endregionprivateButtonbutton1;privateTextBoxtextBox1;privateComboBoxcomboBox1;privateListBoxlistBox1;privateLabellabel1;privateTreeViewtreeView1;privateDataGridViewdataGridView1;privatePictureBoxpictureBox1;privateRadioButtonradioButton1;privateCheckBoxcheckBox1;privateDataGridViewTextBoxColumngridId;privateDataGridViewTextBoxColumngridName;}} 一个class中的成员可以是字段,属性,方法,构造函数,析构函数,事件,索引器,重载运算符,常量,内部嵌套类。通常,一个类库中的class,属性和方法是最常见的,到可视化窗体或控件类时,属性和事件是最常见的,这是因为通过电脑输入设备或内部的动作,会触发很多事件,这些事件会调用我们订阅的方法,从而完成业务的流转。所以学习可视化控件,就是学习他的属性和事件。每个控件,双击后自动订阅的事件就是这个控件最常用的控件,比如Button的Click,TextBox的TextChanged事件等。 有一些控件还有子控件或子选项,就像一个类型的一个属性是集合类型一些,可以添加子类型。有一些控件是容器控件,用来承载其他控件的,它有一个Controles的属性,可以添加其他类型的控件。 关于每个控件的使用这里就不展开了,因为相关的资料很多,官方的文档就是不错的选择。 在WinForm中,大部分事件都是以xxxEventHandler作为定义事件的委托,并且有两个参数,第一个是objectsender,就是发出事件的控件,第二个参数是EventArgs或它的子类,如果是它子类,会携带一些事件的参数。xxxEventHandler和xxxEventArgs与EventHandler和EventArgs是委托和参数的关系,也是对应的。privatevoiddataGridView1CellContentClick(objectsender,DataGridViewCellEventArgse){} 比如DataGridViewCellEventHandler和DataGridViewCellEventArgs,并且Args中有当前单元格的行与列下标。 学习控件是一个细活,一个一个过,关注他们的属性,事件,虽然方法比较少,但也是要关注的,就像研究一个类,就要看他的成员都有什么,各自作用是什么一样。