当前位置:网站首页>WinForm (5) control and its members
WinForm (5) control and its members
2022-08-11 00:50:00 【dotNET cross-platform】
Form is undoubtedlyWinForm的主角,Use one for each formclass来承载,Then the controls of the form,It's a private field in a class.There are three files for each form,两个.cs文件,is a partial class,Designer.cs是自动生成的C#代码,Generally, the code generated after dragging and dropping the control;另一个.csThe file is used to write business code;第三个是.resx文件,is a resource source,picture of form,图标,And some control configuration information.
For example the form below:
The generated code is as follows so much,It can be seen that the designer has contributed a lot.
注:The code below is just to show you how long it is,不需要仔细研究.
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;
}
}
一个classThe members in can be fields,属性,方法,构造函数,析构函数,事件,索引器,重载运算符,常量,内部嵌套类.通常,in a class libraryclass,Properties and methods are the most common,When visualizing a form or control class,Properties and events are the most common,This is due to the action of the device or internal input through the computer,会触发很多事件,These events call the methods we subscribe to,So as to complete the flow of business.So learn visual controls,It is to learn his attributes and events.每个控件,The event that is automatically subscribed after double-clicking is the most commonly used control for this control,比如Button的Click,TextBox的TextChanged事件等.
Some controls have sub-controls or sub-options,Just like a property of a type is some collection type,Subtypes can be added.Some controls are container controls,Used to host other controls,它有一个Controles的属性,Other types of controls can be added.
The use of each control will not be expanded here,Because there is a lot of relevant information,Official documentation is a good choice.
在WinForm中,Most events arexxxEventHandlerAs a delegate that defines the event,并且有两个参数,第一个是object sender,It is the control that emits the event,第二个参数是EventArgs或它的子类,If it is a subclass of it,Will carry some event parameters.xxxEventHandler和xxxEventArgs与EventHandler和EventArgsIs the relationship between delegates and parameters,也是对应的.
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
比如DataGridViewCellEventHandler和DataGridViewCellEventArgs,并且ArgsThere are row and column subscripts of the current cell.
Learning controls is a delicate job,一个一个过,Pay attention to their properties,事件,Although there are few methods,But also pay attention,It's like studying a class,Depends on what his members have,What is the role of each.
边栏推荐
猜你喜欢
C# using timer
Pico 4更多参数曝光:Pancake+彩色透视,还有Pro版本
分库分表ShardingSphere-JDBC笔记整理
YOLOv5的Tricks | 【Trick10】从PyTorch Hub加载YOLOv5
Web APIs BOM - A Comprehensive Case of Operating Browsers
异常和异常处理机制
[GXYCTF2019]BabySQli
时间戳转换为日期格式、获取当前时间戳
rhel7.0解决yum无法使用(system is not registered to Red Hat Subscription Management)
两个链表的第一个公共节点——LeetCode
随机推荐
双机热备综合实验(VRRP+OSPF+VTP+NAT+DHCP+PVSTP+单臂路由)
【ASM】字节码操作 ClassWriter COMPUTE_FRAMES 的作用 与 visitMaxs 的关系
关于科研学习中的几个问题:如何看论文?如何评价工作?如何找idea?
小程序onPageNotFound的坑
两个数组的交集
dump_stack ()
报错:Client does not support authentication protocol requested by server; consider upgrading MySQL cli
electron -autoUpdater 更新
Difference Between Image Recognition and Semantic Segmentation
How to do patent mining, the key is to find patent points, in fact, it is not too difficult
【C语言】探索数据的存储(整形篇)
力扣------值相等的最小索引
详解JDBC的实现与优化(万字详解)
YOLOv5的Tricks | 【Trick11】在线模型训练可视化工具wandb(Weights & Biases)
【服务器数据恢复】raid5崩溃导致lvm信息和VXFS文件系统损坏的数据恢复案例
networkmanager无法打开
Shell 文本三剑客 Sed
How to easily obtain the citation format of references?
ArcGIS Pro 创建tpk
【pypdf2】合并PDF、旋转、缩放、裁剪、加密解密、添加水印