亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 學院 > 開發設計 > 正文

RadioBox、CheckBox和Validating事件的相關處理

2019-11-18 16:51:22
字體:
來源:轉載
供稿:網友

[示例出處]:本示例來自《C#入門經典》第三版中文版,P349-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;
  9
 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();
 19            }
 20            base.Dispose(disposing);
 21        }
 22
 23        Windows 窗體設計器生成的代碼#region Windows 窗體設計器生成的代碼
 24
 25        /**//// <summary>
 26        /// 設計器支持所需的方法 - 不要
 27        /// 使用代碼編輯器修改此方法的內容。
 28        /// </summary>
 29        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();
 39            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
 49            //
 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(59, 12);
 54            this.label1.TabIndex = 0;
 55            this.label1.Text = "labelName";
 56            //
 57            // label2
 58            //
 59            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)
 69                        | 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)));
 79            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)
 89                        | System.Windows.Forms.AnchorStyles.Right)));
 90            this.textBoxAge.Location = new System.Drawing.Point(127, 254);
 91            this.textBoxAge.Name = "textBoxAge";
 92            this.textBoxAge.Size = new System.Drawing.Size(99, 21);
 93            this.textBoxAge.TabIndex = 4;
 94            this.textBoxAge.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBoxAge_KeyPress);
 95            //
 96            // textBoxOutput
 97            //
 98            this.textBoxOutput.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
 99                        | 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            //
109            // label4
110            //
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            // label5
119            //
120            this.label5.AutoSize = true;
121            this.label5.Location = new System.Drawing.Point(26, 289);
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            // buttonOK
128            //
129            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 = 9;
134            this.buttonOK.Text = "OK";
135            this.buttonOK.UseVisualStyleBackColor = true;
136            this.buttonOK.Click += new System.EventHandler(this.buttonOK_Click);
137            //
138            // buttonHelp
139            //
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);
149            //
150            // checkBoxProgrammer
151            //
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;
159            this.checkBoxProgrammer.Text = "Programmer";
160            this.checkBoxProgrammer.UseVisualStyleBackColor = true;
161            //
162            // groupBoxSex
163            //
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);
169            this.groupBoxSex.TabIndex = 12;
170            this.groupBoxSex.TabStop = false;
171            this.groupBoxSex.Text = "Sex";
172            //
173            // radioButtonMale
174            //
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);
179            this.radioButtonMale.TabIndex = 1;
180            this.radioButtonMale.Text = "radioButtonMale";
181            this.radioButtonMale.UseVisualStyleBackColor = true;
182            //
183            // radioButtonFemale
184            //
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";
189            this.radioButtonFemale.Size = new System.Drawing.Size(125, 16);
190            this.radioButtonFemale.TabIndex = 0;
191            this.radioButtonFemale.TabStop = true;
192            this.radioButtonFemale.Text = "radioButtonFemale";
193            this.radioButtonFemale.UseVisualStyleBackColor = true;
194            //
195            // Form1
196            //
197            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
198            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
199            this.ClientSize = new System.Drawing.Size(561, 490);
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);
209            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();
219
220        }
221
222        #endregion
223
224        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;
229        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    }
239}
240
241
Form1.cs
  1using System;
  2using System.Collections.Generic;
  3using System.ComponentModel;
  4using System.Data;
  5using System.Drawing;
  6using System.Text;
  7using System.Windows.Forms;
  8
  9namespace 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.
 19            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事件。
 29            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           
 39            //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 ?
 49                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.
 59            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;
 69        }
 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)
 79            {
 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;
 89                tb.Tag = true;
 90            }
 91            //Finally ,we call ValidateOK which will set the value of the OK button.
 92            ValidateOK();
 93        }
 94
 95        private void textBoxAge_KeyPress(object sender, KeyPressEventArgs e)
 96        {
 97            if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8)
 98            {
 99                e.Handled = true;   //Remove the character
100                //等于true告訴用戶不應該對字符進行任何操作
101            }
102        }
103
104        private void textBox_TextChanged(object sender, EventArgs e)
105        {
106            //Cast the sender object to a TextBox
107            TextBox tb = (TextBox)sender;
108
109            //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            else
116            {
117                tb.Tag = true;
118                tb.BackColor = SystemColors.Window;
119            }
120
121            //Call ValidateOK to set the OK button
122            ValidateOK();
123        }
124
125        private void ValidateOK()
126        {
127            //Set the OK button to enabled if all the Tags are true
128            this.buttonOK.Enabled = ((bool)(this.textBoxName.Tag) &&
129                (bool)(this.textBoxAge.Tag) &&
130                (bool)(this.textBoxAddress.Tag));
131        }
132    }
133}
[示例說明]:
1、開發語言:C#
2、開發環境:Visual Studio.Net 2005 Team suite
3、開發模板:C#.net項目->Windows應用程序
http://www.49028c.com/volnet/archive/2006/11/27/574437.html


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
国产精品电影一区| www.国产一区| 国产丝袜一区二区三区免费视频| 亚洲精品99久久久久中文字幕| 激情久久av一区av二区av三区| 2018日韩中文字幕| 日韩欧美精品中文字幕| 不卡av在线播放| 久操成人在线视频| 国产一区二区免费| 欧美日韩国产精品专区| 亚洲免费中文字幕| 国产精品久久久久久久9999| 日韩电影中文字幕在线观看| 亚洲韩国日本中文字幕| 成人网欧美在线视频| 国产精品999999| 97国产精品久久| 日韩小视频网址| 亚洲2020天天堂在线观看| 色综合久久久888| 亚洲国产精品成人va在线观看| 欧美日本中文字幕| 91夜夜揉人人捏人人添红杏| 国产婷婷色综合av蜜臀av| 中文字幕视频在线免费欧美日韩综合在线看| 国产精品视频网| 日韩视频免费观看| 98精品国产高清在线xxxx天堂| 久久久免费观看视频| 久久久亚洲网站| 久久av红桃一区二区小说| 欧美日韩福利视频| 欧美一级黑人aaaaaaa做受| 久久久精品2019中文字幕神马| 色综合影院在线| 97视频com| 国产一区香蕉久久| 欧美一级视频免费在线观看| 色婷婷综合久久久久| 欧美又大又硬又粗bbbbb| 久久久久久久久久久免费精品| 亚洲成人网久久久| 一区二区在线视频| 亚洲国产成人久久综合一区| 97香蕉超级碰碰久久免费的优势| 4p变态网欧美系列| 最新国产成人av网站网址麻豆| 精品视频久久久久久久| 亚洲国产一区自拍| 中文字幕在线视频日韩| 狠狠色香婷婷久久亚洲精品| 国产一区二中文字幕在线看| 欧美日韩国产精品专区| 最新国产精品拍自在线播放| 亚洲色图美腿丝袜| 欧美一级高清免费播放| 日韩亚洲成人av在线| 日韩在线观看免费高清| 欧美激情图片区| 欧美成人亚洲成人日韩成人| 国产精品激情av在线播放| 久久99精品国产99久久6尤物| 久久国产精品亚洲| 4k岛国日韩精品**专区| 日本伊人精品一区二区三区介绍| 久久青草精品视频免费观看| 欧美激情视频在线免费观看 欧美视频免费一| 中文字幕国内精品| 久久精品国亚洲| 亚洲视频axxx| 欧美另类交人妖| 欧美另类老女人| 成人精品视频99在线观看免费| 国产999在线观看| 亚洲男人的天堂网站| 国产+成+人+亚洲欧洲| 孩xxxx性bbbb欧美| 亚洲一区二区三区在线视频| 琪琪亚洲精品午夜在线| 亚洲色图激情小说| 日韩欧中文字幕| 欧美在线观看网址综合| 久久久精品一区| 在线观看欧美视频| 久久不射热爱视频精品| 久久91超碰青草是什么| 亚洲欧美另类在线观看| 久久久av电影| 91丨九色丨国产在线| 欧美日韩免费看| 国产亚洲精品久久久久动| 国产精品久久久久久久久久久久久| 精品人伦一区二区三区蜜桃免费| 91日本在线视频| 欧美精品一区在线播放| 日韩毛片在线观看| 久久久久亚洲精品成人网小说| 91免费福利视频| 亚洲精品国产精品国自产观看浪潮| 国产精品99导航| 欧美中文在线免费| 26uuu日韩精品一区二区| 欧美黑人一级爽快片淫片高清| 欧美电影免费观看网站| 美女扒开尿口让男人操亚洲视频网站| 综合激情国产一区| 欧美日韩精品中文字幕| 日本精品中文字幕| 不卡中文字幕av| 欧美激情中文字幕乱码免费| 国产精品久久久久久久午夜| 亚洲日本aⅴ片在线观看香蕉| 国产精欧美一区二区三区| 97国产在线观看| 日本久久中文字幕| 4k岛国日韩精品**专区| www.亚洲成人| 国内精品免费午夜毛片| 亚洲欧洲中文天堂| 国产欧美日韩综合精品| 欧美高清视频在线播放| 欧美成人手机在线| 成人做爰www免费看视频网站| 国产精品久久久久久久久久久久久久| 中文字幕一区电影| 欧美日韩中文字幕在线| 亚洲精品一二区| 亚洲精品wwww| 久久伊人精品天天| 欧美激情精品久久久久久蜜臀| 中文字幕不卡在线视频极品| 国产91在线视频| 国产精品久久久久免费a∨| 欧美日韩激情美女| 久久久久久亚洲精品| 日韩黄色在线免费观看| 91精品国产91久久久久久吃药| 日韩精品高清视频| 97av在线影院| 欧美精品性视频| 精品亚洲一区二区三区在线观看| 国产精品久久激情| 国产视频精品一区二区三区| 影音先锋欧美在线资源| 青青草原一区二区| 57pao成人国产永久免费| 国模精品视频一区二区三区| 日韩专区中文字幕| 亚洲肉体裸体xxxx137| 亚洲二区在线播放视频| 亚洲欧美另类自拍| 欧美日韩亚洲一区二区三区| 国产亚洲欧洲在线| 日本人成精品视频在线| 神马久久桃色视频| 亚洲人线精品午夜| 欧美成人全部免费| 成人有码视频在线播放| 欧美在线欧美在线| 亚洲伊人久久综合| 欧美人成在线视频| 亚洲四色影视在线观看| 5566成人精品视频免费|