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.pswp.cn/news/283858.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/283858.shtml
英文地址,請注明出處:http://en.pswp.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;但實際…