[示例出处]:本示例来自《C#入门经典》第三版中文版,P34Array-P353[示例涉及]:1、RadioBox、CheckBox控件的基本使用2、Validating事件的使用(同[C#][SAMPLE][CODE][Control]TextBox和Validating事件的相关处理 )3、多委托处理同一事件方法(同[C#][SAMPLE][CODE][Control]TextBox和Validating事件的相关处理 )[示例代码]:2文件(其余默认)
Form1.Designer.cs; 1namespace WA_TextBoxTest; 2{; 3;partial class Form1; 4;{; 5;;;;;/**//// <summary>; 6;;;;;/// 必需的设计器变量。; 7;;;;;/// </summary>; 8;;;;;private System.ComponentModel.IContainer components = null;; Array;10;;;;;/**//// <summary>;11;;;;;/// 清理所有正在使用的资源。;12;;;;;/// </summary>;13;;;;;/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>;14;;;;;protected override void Dispose(bool disposing);15;;;;;{;16;if (disposing && (components != null));17;{;18;;;;;components.Dispose();;1Array;};20;base.Dispose(disposing);;21;;;;;};22;23;;;;;Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码;24;25;;;;;/**//// <summary>;26;;;;;/// 设计器支持所需的方法 - 不要;27;;;;;/// 使用代码编辑器修改此方法的内容。;28;;;;;/// </summary>;2Array;;;;;private void InitializeComponent();30;;;;;{;31;this.label1 = new System.Windows.Forms.Label();;32;this.label2 = new System.Windows.Forms.Label();;33;this.textBoxName = new System.Windows.Forms.TextBox();;34;this.textBoxAddress = new System.Windows.Forms.TextBox();;35;this.textBoxAge = new System.Windows.Forms.TextBox();;36;this.textBoxOutput = new System.Windows.Forms.TextBox();;37;this.label4 = new System.Windows.Forms.Label();;38;this.label5 = new System.Windows.Forms.Label();;3Array;this.buttonOK = new System.Windows.Forms.Button();;40;this.buttonHelp = new System.Windows.Forms.Button();;41;this.checkBoxProgrammer = new System.Windows.Forms.CheckBox();;42;this.groupBoxSex = new System.Windows.Forms.GroupBox();;43;this.radioButtonMale = new System.Windows.Forms.RadioButton();;44;this.radioButtonFemale = new System.Windows.Forms.RadioButton();;45;this.groupBoxSex.SuspendLayout();;46;this.SuspendLayout();;47;// ;48;// label1;4Array;// ;50;this.label1.AutoSize = true;;51;this.label1.Location = new System.Drawing.Point(26, 27);;52;this.label1.Name = "label1";;53;this.label1.Size = new System.Drawing.Size(5Array, 12);;54;this.label1.TabIndex = 0;;55;this.label1.Text = "labelName";;56;// ;57;// label2;58;// ;5Array;this.label2.AutoSize = true;;60;this.label2.Location = new System.Drawing.Point(26, 53);;61;this.label2.Name = "label2";;62;this.label2.Size = new System.Drawing.Size(77, 12);;63;this.label2.TabIndex = 1;;64;this.label2.Text = "labelAddress";;65;// ;66;// textBoxName;67;// ;68;this.textBoxName.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left);6Array;;;;;| System.Windows.Forms.AnchorStyles.Right)));;70;this.textBoxName.Location = new System.Drawing.Point(127, 24);;71;this.textBoxName.Name = "textBoxName";;72;this.textBoxName.Size = new System.Drawing.Size(317, 21);;73;this.textBoxName.TabIndex = 2;;74;// ;75;// textBoxAddress;76;// ;77;this.textBoxAddress.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left);78;;;;;| System.Windows.Forms.AnchorStyles.Right)));;7Array;this.textBoxAddress.Location = new System.Drawing.Point(127, 50);;80;this.textBoxAddress.Multiline = true;;81;this.textBoxAddress.Name = "textBoxAddress";;82;this.textBoxAddress.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;;83;this.textBoxAddress.Size = new System.Drawing.Size(317, 102);;84;this.textBoxAddress.TabIndex = 2;;85;// ;86;// textBoxAge;87;// ;88;this.textBoxAge.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left);8Array;;;;;| System.Windows.Forms.AnchorStyles.Right)));;Array0;this.textBoxAge.Location = new System.Drawing.Point(127, 254);;Array1;this.textBoxAge.Name = "textBoxAge";;Array2;this.textBoxAge.Size = new System.Drawing.Size(ArrayArray, 21);;Array3;this.textBoxAge.TabIndex = 4;;Array4;this.textBoxAge.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBoxAge_KeyPress);;Array5;// ;Array6;// textBoxOutput;Array7;// ;Array8;this.textBoxOutput.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom);ArrayArray;;;;;| System.Windows.Forms.AnchorStyles.Left)100;;;;;| System.Windows.Forms.AnchorStyles.Right)));101;this.textBoxOutput.Location = new System.Drawing.Point(28, 307);102;this.textBoxOutput.Multiline = true;103;this.textBoxOutput.Name = "textBoxOutput";104;this.textBoxOutput.ReadOnly = true;105;this.textBoxOutput.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;106;this.textBoxOutput.Size = new System.Drawing.Size(416, 171);107;this.textBoxOutput.TabIndex = 5;108;// 10Array;// label4110;// 111;this.label4.AutoSize = true;112;this.label4.Location = new System.Drawing.Point(26, 257);113;this.label4.Name = "label4";114;this.label4.Size = new System.Drawing.Size(53, 12);115;this.label4.TabIndex = 7;116;this.label4.Text = "labelAge";117;// 118;// label511Array;// 120;this.label5.AutoSize = true;121;this.label5.Location = new System.Drawing.Point(26, 28Array);122;this.label5.Name = "label5";123;this.label5.Size = new System.Drawing.Size(71, 12);124;this.label5.TabIndex = 8;125;this.label5.Text = "labelOutput";126;// 127;// buttonOK128;// 12Array;this.buttonOK.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));130;this.buttonOK.Location = new System.Drawing.Point(458, 18);131;this.buttonOK.Name = "buttonOK";132;this.buttonOK.Size = new System.Drawing.Size(82, 21);133;this.buttonOK.TabIndex = Array;134;this.buttonOK.Text = "OK";135;this.buttonOK.UseVisualStyleBackColor = true;136;this.buttonOK.Click += new System.EventHandler(this.buttonOK_Click);137;// 138;// buttonHelp13Array;// 140;this.buttonHelp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));141;this.buttonHelp.CausesValidation = false;142;this.buttonHelp.Location = new System.Drawing.Point(458, 43);143;this.buttonHelp.Name = "buttonHelp";144;this.buttonHelp.Size = new System.Drawing.Size(82, 21);145;this.buttonHelp.TabIndex = 10;146;this.buttonHelp.Text = "Help";147;this.buttonHelp.UseVisualStyleBackColor = true;148;this.buttonHelp.Click += new System.EventHandler(this.buttonHelp_Click);14Array;// 150;// checkBoxProgrammer151;// 152;this.checkBoxProgrammer.AutoSize = true;153;this.checkBoxProgrammer.Checked = true;154;this.checkBoxProgrammer.CheckState = System.Windows.Forms.CheckState.Checked;155;this.checkBoxProgrammer.Location = new System.Drawing.Point(28, 166);156;this.checkBoxProgrammer.Name = "checkBoxProgrammer";157;this.checkBoxProgrammer.Size = new System.Drawing.Size(84, 16);158;this.checkBoxProgrammer.TabIndex = 11;15Array;this.checkBoxProgrammer.Text = "Programmer";160;this.checkBoxProgrammer.UseVisualStyleBackColor = true;161;// 162;// groupBoxSex163;// 164;this.groupBoxSex.Controls.Add(this.radioButtonMale);165;this.groupBoxSex.Controls.Add(this.radioButtonFemale);166;this.groupBoxSex.Location = new System.Drawing.Point(28, 188);167;this.groupBoxSex.Name = "groupBoxSex";168;this.groupBoxSex.Size = new System.Drawing.Size(415, 55);16Array;this.groupBoxSex.TabIndex = 12;170;this.groupBoxSex.TabStop = false;171;this.groupBoxSex.Text = "Sex";172;// 173;// radioButtonMale174;// 175;this.radioButtonMale.AutoSize = true;176;this.radioButtonMale.Location = new System.Drawing.Point(235, 24);177;this.radioButtonMale.Name = "radioButtonMale";178;this.radioButtonMale.Size = new System.Drawing.Size(113, 16);17Array;this.radioButtonMale.TabIndex = 1;180;this.radioButtonMale.Text = "radioButtonMale";181;this.radioButtonMale.UseVisualStyleBackColor = true;182;// 183;// radioButtonFemale184;// 185;this.radioButtonFemale.AutoSize = true;186;this.radioButtonFemale.Checked = true;187;this.radioButtonFemale.Location = new System.Drawing.Point(55, 24);188;this.radioButtonFemale.Name = "radioButtonFemale";18Array;this.radioButtonFemale.Size = new System.Drawing.Size(125, 16);1Array0;this.radioButtonFemale.TabIndex = 0;1Array1;this.radioButtonFemale.TabStop = true;1Array2;this.radioButtonFemale.Text = "radioButtonFemale";1Array3;this.radioButtonFemale.UseVisualStyleBackColor = true;1Array4;// 1Array5;// Form11Array6;// 1Array7;this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);1Array8;this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;1ArrayArray;this.ClientSize = new System.Drawing.Size(561, 4Array0);200;this.Controls.Add(this.groupBoxSex);201;this.Controls.Add(this.checkBoxProgrammer);202;this.Controls.Add(this.buttonHelp);203;this.Controls.Add(this.buttonOK);204;this.Controls.Add(this.label5);205;this.Controls.Add(this.label4);206;this.Controls.Add(this.textBoxOutput);207;this.Controls.Add(this.textBoxAge);208;this.Controls.Add(this.textBoxAddress);20Array;this.Controls.Add(this.textBoxName);210;this.Controls.Add(this.label2);211;this.Controls.Add(this.label1);212;this.Name = "Form1";213;this.Text = "Form1";214;this.Validating += new System.ComponentModel.CancelEventHandler(this.textBoxEmpty_Validating);215;this.groupBoxSex.ResumeLayout(false);216;this.groupBoxSex.PerformLayout();217;this.ResumeLayout(false);218;this.PerformLayout();21Array220;;;;;}221222;;;;;#endregion223224;;;;;private System.Windows.Forms.Label label1;225;;;;;private System.Windows.Forms.Label label2;226;;;;;private System.Windows.Forms.TextBox textBoxName;227;;;;;private System.Windows.Forms.TextBox textBoxAddress;228;;;;;private System.Windows.Forms.TextBox textBoxAge;22Array;;;;;private System.Windows.Forms.TextBox textBoxOutput;230;;;;;private System.Windows.Forms.Label label4;231;;;;;private System.Windows.Forms.Label label5;232;;;;;private System.Windows.Forms.Button buttonOK;233;;;;;private System.Windows.Forms.Button buttonHelp;234;;;;;private System.Windows.Forms.CheckBox checkBoxProgrammer;235;;;;;private System.Windows.Forms.GroupBox groupBoxSex;236;;;;;private System.Windows.Forms.RadioButton radioButtonMale;237;;;;;private System.Windows.Forms.RadioButton radioButtonFemale;238;}23Array}240241Form1.cs; 1using System;; 2using System.Collections.Generic;; 3using System.ComponentModel;; 4using System.Data;; 5using System.Drawing;; 6using System.Text;; 7using System.Windows.Forms;; 8; Arraynamespace WA_TextBoxTest;10{;11;public partial class Form1 : Form;12;{;13;;;;;public Form1();14;;;;;{;15;InitializeComponent();;16;this.buttonOK.Enabled = false;;17;18;//Tag values for testing if the data is valid.;1Array;this.textBoxName.Tag = false;;20;this.textBoxAddress.Tag = false;;21;this.textBoxAge.Tag = false;;22;23;//订阅事件Subscriptions to events;24;this.textBoxName.Validating += new CancelEventHandler(this.textBoxEmpty_Validating);;25;this.textBoxAddress.Validating += new CancelEventHandler(this.textBoxEmpty_Validating);;26;this.textBoxAge.Validating += new CancelEventHandler(this.textBoxEmpty_Validating);;27;28;//当控件中文本发生改变,就激发TextChanged事件。;2Array;this.textBoxName.TextChanged +=new EventHandler(textBox_TextChanged);;30;this.textBoxAddress.TextChanged+=new EventHandler(textBox_TextChanged);;31;this.textBoxAge.TextChanged += new EventHandler(textBox_TextChanged);;32;;;;;};33;34;;;;;private void buttonOK_Click(object sender, EventArgs e);35;;;;;{;36;//No testing for invalid values are made ,as that should not be necessary;37;string output;;38;;3Array;//Concatenate the text values of for TextBoxes.;40;output = "Name:" + this.textBoxName.Text + "\r\n";;41;output += "Address:" + this.textBoxAddress.Text + "\r\n";;42;output += "Occupation:" + (string)(this.checkBoxProgrammer.Checked ?;43;;;;;"Programmer" : "Not a programmer") + "\r\n";;//此句等价于下面这句;44;output += "Occupation:" + (string)(this.checkBoxProgrammer.Checked ?;45;;;;;this.checkBoxProgrammer.Text : ("Not a "+this.checkBoxProgrammer.Text)) + "\r\n";;46;output += "Sex:" + (string)(this.radioButtonFemale.Checked ?;47;;;;;"Female" : "Male") + "\r\n";;//此句等价于下面这句;48;output += "Sex:" + (string)(this.radioButtonFemale.Checked ?;4Array;;;;;this.radioButtonFemale.Text : this.radioButtonMale.Text) + "\r\n";;50;output += "Age:" + this.textBoxAge.Text;;51 ;52;//Insert the new text.;53;this.textBoxOutput.Text = output;;54;;;;;};55;56;;;;;private void buttonHelp_Click(object sender, EventArgs e);57;;;;;{;58;//Write a short description of each TextBox in the Output TextBox.;5Array;string output;;60;61;output = "Name=Your name\r\n";;62;output += "Address=Your address\r\n";;63;output += "Programmer=Check Programmer if you are a programmer\r\n";;64;output += "Sex=Choose your sex\r\n";;65;output += "Age=Your age";;66;67;//Insert the new text.;68;this.textBoxOutput.Text = output;;6Array;;;;;};70;71;;;;;private void textBoxEmpty_Validating(object sender, CancelEventArgs e);72;;;;;{;73;//我们知道这个sender是一个对话框,所以我们将他们强制转换为TextBox;74;TextBox tb = (TextBox)sender;;75;76;//如果对话框是空的话我们设置TextBox背景色为红色来象征问题。;77;//如果控件有valid信息,我们就使用控件的Tag值来指出。;78;if (tb.Text.Length == 0);7Array;{;80;;;;;tb.BackColor = Color.Red;;81;;;;;tb.Tag = false;;82;;;;;//在这个例子中我们不想取消further processing;83;;;;;//但是如果我们想要这么做的话,我们只需要添加以下一行:;84;;;;;//e.Cancel=true;;85;};86;else;87;{;88;;;;;this.BackColor = SystemColors.Window;;8Array;;;;;tb.Tag = true;;Array0;};Array1;//Finally ,we call ValidateOK which will set the value of the OK button.;Array2;ValidateOK();;Array3;;;;;};Array4;Array5;;;;;private void textBoxAge_KeyPress(object sender, KeyPressEventArgs e);Array6;;;;;{;Array7;if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8);Array8;{;ArrayArray;;;;;e.Handled = true;//Remove the character100;;;;;//等于true告诉用户不应该对字符进行任何操作101;}102;;;;;}103104;;;;;private void textBox_TextChanged(object sender, EventArgs e)105;;;;;{106;//Cast the sender object to a TextBox107;TextBox tb = (TextBox)sender;10810Array;//Test if the data is valid and set the tag and back ground color accordingly.110;if (tb.Text.Length == 0)111;{112;;;;;tb.Tag = false;113;;;;;tb.BackColor = Color.Red;114;}115;else116;{117;;;;;tb.Tag = true;118;;;;;tb.BackColor = SystemColors.Window;11Array;}120121;//Call ValidateOK to set the OK button122;ValidateOK();123;;;;;}124125;;;;;private void ValidateOK()126;;;;;{ 127;//Set the OK button to enabled if all the Tags are true128;this.buttonOK.Enabled = ((bool)(this.textBoxName.Tag) &&12Array;;;;;(bool)(this.textBoxAge.Tag) &&130;;;;;(bool)(this.textBoxAddress.Tag));131;;;;;}132;}133}[示例说明]:1、开发语言:C#2、开发环境:Visual Studio.Net 2005 Team suite3、开发模板:C#.net项目->Windows应用程序