首页 > 其他分享 >WinForm(五)控件和它的成员

WinForm(五)控件和它的成员

时间:2022-12-11 18:55:24浏览次数:51  
标签:控件 Windows 成员 System Forms new Drawing 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中有当前单元格的行与列下标。

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

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

标签:控件,Windows,成员,System,Forms,new,Drawing,WinForm
From: https://www.cnblogs.com/axzxs2001/p/16974166.html

相关文章

  • WinForm(四)一种实现登录的方式
    首先声明,这只是一种登录方式,并不是最好的方式,用这个例子为了说明登录窗体和Application的关系。在登录前,定义了用户实体,然后是一个通用的类,存放进程中当前登录的......
  • 单选与多选控件的简单应用
    这里不好理解的是选择项分组,按Ctrl+D快捷键,先设置按钮Tab顺序,对编排界面的元素Tab顺序,假Tab序号为1~3的分为一组,4~6分为一组,那么设置序号1的选择项属性杂项->组=Tr......
  • 如何使Winform的Textbox滚动条保持在最后
    Winform编程中,使用的Textbox控件,我们很有可能会遇到需要随时更新其内容(比如聊天窗口文字的更新),当内容过多的时候,就会出现滚动条,如何让滚动条时刻跟随内容的最下面内容呢? ......
  • TabControl控件的简单使用-添加tab
    1、首先创建一个MFC对话框框架,在对话框资源上从工具箱中添加上一个TabControl控件,根据需要修改一下属性,然后右击控件,为这个控件添加一个变量,将此控件跟一个CTabCtrl类变......
  • 为文本框控件添加滚动条-CEdit
    在VS2015环境下操作创建文本框控件设置控件属性效果......
  • 重走Android路 之 挑几个基本控件玩玩(上卷)
    LZ-Says:冬瓜嫌弃LZ这个系列,说他都能看懂,哎呦我去,这个给人气的呀~真想把冬瓜剁吧剁吧熬成粥~高考了,祝愿各位学子功成归来~!!!基本控件使用Android为我们提供了很多UI样式,但是在......
  • winform 实现文档预览。
      文档预览一直是一个问题,尤其是web网站,实现文档预览,淡然如果你付费第三方,当我没说。微软给的接口实现预览效果还可以,但是他又限制,就是ip不行,需要域名。这个就有点苦逼......
  • 【Chart控件】折线图
    多Y轴显示效果: <Windowx:Class="Chart.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://sche......
  • 【Chart控件】开篇
    WPF没有系统的Chart控件,因此要使用第三方的chart控件库1、在Nuget中添加DotNetProjects.DataVisualization.Toolkit。Chart控件结构        ......
  • 【Chart控件】饼状图
    WPF没有系统的Chart控件,因此要使用第三方的chart控件库1、在Nuget中添加DotNetProjects.DataVisualization.Toolkit。Chart控件结构饼状图效果  xaml<Window......