WinForm(五)控件和它的成员

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

比如下面的窗体:

74a5802d19d143e69b549413ba40ddf1.png

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

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

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

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/283858.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Atitit.异常处理 嵌套  冗长的解决方案

Atitit.异常处理 嵌套 冗长的解决方案 1. 异常处理的需要改进的地方1 2. 异常设计的初衷是, 在程序中出现错误时, 由程序自己处理错误, 尽量不要以exit(0)这种粗暴的方式中止程序. 1 3. 正常流程和异常流程的分离。2 4. “是药三分毒”&#xff0c; 任何事物有缺点&#xff0c…

一文详解|增长那些事儿

目录 增长的背景 1.1 增长的定义 1.2 如何判断事物是否在增长 1.3 如何判断事物能否持续增长 如何进行增长 2.1 寻找增长机会点&#xff08;人的能力&#xff09; 2.1.1 发散与收剑找机会点 2.1.2 实验分析验证 2.1.3 增长洞察提取策略 2.1.4 如何找到大机会 2.2 设…

在MVC项目中使用Ninject

项目结构图&#xff1a; App_start文件夹中的文件是VS自己创建的&#xff0c;其中NinjectWebCommon类在创建之初并不存在。后面会再次提到&#xff01; 添加一个Home控制器。代码如下&#xff1a; using EssentialTools.Models; using Ninject; using System; using System.Col…

linux IP、端口连通性测试

ssh -v -p 50001 root10.210.200.82转载于:https://www.cnblogs.com/kuiyeit/p/6723508.html

紧急通知:360 网站卫士前端公共库已停止服务

所有使用了360前端公共库的开发者和站长们&#xff0c;请及时更换你的前端库的链接&#xff08;主要是前端库和谷歌 fonts&#xff09;&#xff0c;否则网站打开速度会极慢&#xff0c;甚至会在 Chrome 浏览器中崩溃。 360前端公共库曾经提供的服务有&#xff1a; 前端公共库&a…

一文学会Autofac的基础操作:几种实现注册方式、3种注入方式、生命周期、AOP以及过滤器实现依赖注入...

前言&#xff1a;直接开干。使用Autofac进行服务注册实践&#xff1a;新建三个项目&#xff0c;分别是webapi项目 Wesky.Core.Autofac以及两个类库项目 Wesky.Core.Interface和Wesky.Core.Service。在Webapi项目下&#xff0c;引用Autofac的三个包&#xff1a;Autofac、Autofac…

解析互联网广告术语 CPM、CPC、CPA、CPS、CPL、CPR 是什么意思

1. CPM&#xff08;Cost per mille&#xff09;&#xff0c;每千次展现收费 这是一种最为常见的广告模式&#xff0c;也是很多网站流量变现的一种途径&#xff0c;这种广告不管计算点击&#xff0c;或者什么注册下载之类的转化&#xff0c;只要这个广告在网站上被正常的展现给…

JavaScript数组迭代方法(图解)

转载于:https://www.cnblogs.com/seanna/p/6724032.html

Rider调试ASP.NET Core时报thread not gc-safe的解决方法

新建了一个ASP.NET Core 5.0的Web API项目&#xff0c;当使用断点调试Host.CreateDefaultBuilder(args)时&#xff0c;进入该函数后查看中间变量的值&#xff0c;报错Evaluation is not allowed: The thread is not at a GC-safe point。在群里问了也没人回应&#xff0c;可能没…

The SDK platform-tools version ((23)) is too old to check APIs compiled with API 26;

好像是更新过啥SDK之后&#xff0c;项目一直在包名的那一行显示红线&#xff0c;不过是不报编译错误的&#xff0c;就是看着老扎心老扎心的&#xff0c;开始以为是指定的SDK版本的问题&#xff0c;修改后发现无效&#xff0c;最后找到方法解决&#xff1a; 打开SDK Manager ---…

oracle 各种日期函数格式和操作

2019独角兽企业重金招聘Python工程师标准>>> ORACLE日期时间函数大全 TO_DATE格式(以时间:2007-11-02 13:45:25为例) Year: yy two digits 两位年 显示值:07 yyy three digits 三位年 显示值:00…

火山引擎李玉光:字节跳动大规模K8s集群管理实践

2022年5月31日&#xff0c;在CSDN云原生系列在线峰会第6期“K8s大规模应用和深度实践峰会”&#xff0c;火山引擎资深云原生架构师李玉光分享了《字节跳动大规模K8s集群管理实践》。 字节跳动云原生体系 字节跳动内部云原生技术的使用贯穿组织技术体系各层面&#xff0c;整体如…

(7)关于margin的一些想法2.0

这篇主要讨论的就是margin负值与float的关系。 首先&#xff0c;例子。 <!doctype html> <html> <head> <meta charset"utf-8"> <title>无标题文档</title> <style typetext/css> html,body{padding:0;margin:0;} div{wid…

解决ASP.NET Core在Task中使用IServiceProvider的问题

前言问题的起因是在帮同事解决遇到的一个问题&#xff0c;他的本意是在EF Core中为了解决避免多个线程使用同一个DbContext实例的问题。但是由于对Microsoft.Extensions.DependencyInjection体系的深度不是很了解&#xff0c;结果遇到了新的问题&#xff0c;当时整得我也有点蒙…

什么是SRE?一文详解SRE运维体系

在任何有一定规模的企业内部&#xff0c;一旦推行起来整个SRE的运维模式&#xff0c;那么对于可观测性系统的建设将变得尤为重要&#xff0c;而在整个可观测性系统中。 可观测性系统 在任何有一定规模的企业内部&#xff0c;一旦推行起来整个SRE的运维模式&#xff0c;那么对于…

python初探

python近两年似乎已经很热了&#xff0c;不了解一下怎么能行呢&#xff0c;似乎python最大的优点就是简洁、易懂、优雅。目前豆瓣、知乎等后台服务使用的也都是python语言。 python一般可以用于网站服务、小工具、数据分析等工作。它作为高级语言&#xff0c;和js一样&#xff…

Linux系统PATH变量配置

alias命令用于设置命令的别名&#xff0c;格式为“alias 别名命令” 例如担心复制文件时误将文件被覆盖&#xff0c;可以执行alias cp" cp -i"&#xff0c;如此一来 每次复制命令都会询问用户是否要覆盖。 unalias命令用于取消命令的别名&#xff1a;格式为"una…

solr5.5索引mysql数据(新手总结)

一 solr5.5环境部署到Eclipse(luna版&#xff09; solr部署参见&#xff1a;http://blog.csdn.net/csmnjk/article/details/64121765 二 Ik分词器设置 IK分词器设置参见:http://blog.csdn.net/csmnjk/article/details/51693578 solr4版本的schema.xml文件对应solr5版本的manage…

老板加薪!看我做的WPF Loading!!!

老板加薪&#xff01;看我做的WPF Loading&#xff01;&#xff01;&#xff01;控件名&#xff1a;RingLoading作者&#xff1a;WPFDevelopersOrg原文链接&#xff1a; https://github.com/WPFDevelopersOrg/WPFDevelopers.Minimal框架使用大于等于.NET40&#xff1b;Visua…

如何避免下重复订单

电子交易的一个很基本的问题&#xff0c;就是避免用户下重复订单。用户明明想买一次&#xff0c;结果一看下了两个单。如果没有及时发现&#xff0c;就会带来额外的物流成本和扯皮。对商家的信誉也不好看。 从技术上看&#xff0c;这是一个分布式一致性问题&#xff1b;但实际…