[C#][SAMPLE][CODE][Control]TextBox和Validating事件的相关处理
[示例出处]:本示例来自《C#入门经典》第三版中文版,P342-P348
[示例涉及]:
1、TextBox控件的基本使用
2、Validating事件的使用
3、多委托处理同一事件方法
[示例代码]:2文件(其余默认)
Form1.Designer.cs
Form1.cs
[示例说明]:
1、开发语言:C#
2、开发环境:Visual Studio.Net 2005 Team suite
3、开发模板:C#.net项目->Windows应用程序
[示例涉及]:
1、TextBox控件的基本使用
2、Validating事件的使用
3、多委托处理同一事件方法
[示例代码]:2文件(其余默认)
Form1.Designer.cs
1
namespace 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 窗体设计器生成的代码
192
193
private System.Windows.Forms.Label label1;
194
private System.Windows.Forms.Label label2;
195
private System.Windows.Forms.TextBox textBoxName;
196
private System.Windows.Forms.TextBox textBoxAddress;
197
private System.Windows.Forms.TextBox textBoxOccupation;
198
private System.Windows.Forms.TextBox textBoxAge;
199
private System.Windows.Forms.TextBox textBoxOutput;
200
private System.Windows.Forms.Label label3;
201
private System.Windows.Forms.Label label4;
202
private System.Windows.Forms.Label label5;
203
private System.Windows.Forms.Button buttonOK;
204
private System.Windows.Forms.Button buttonHelp;
205
}
206
}
207
208
namespace WA_TextBoxTest2
{3
partial class Form14
{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 窗体设计器生成的代码192

193
private System.Windows.Forms.Label label1;194
private System.Windows.Forms.Label label2;195
private System.Windows.Forms.TextBox textBoxName;196
private System.Windows.Forms.TextBox textBoxAddress;197
private System.Windows.Forms.TextBox textBoxOccupation;198
private System.Windows.Forms.TextBox textBoxAge;199
private System.Windows.Forms.TextBox textBoxOutput;200
private System.Windows.Forms.Label label3;201
private System.Windows.Forms.Label label4;202
private System.Windows.Forms.Label label5;203
private System.Windows.Forms.Button buttonOK;204
private System.Windows.Forms.Button buttonHelp;205
}206
}207

208

Form1.cs
1
using System;
2
using System.Collections.Generic;
3
using System.ComponentModel;
4
using System.Data;
5
using System.Drawing;
6
using System.Text;
7
using System.Windows.Forms;
8
9
namespace 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
this.textBoxOccupation.Tag = false;
23
24
//订阅事件Subscriptions to events
25
this.textBoxName.Validating += new CancelEventHandler(this.textBoxEmpty_Validating);
26
this.textBoxAddress.Validating += new CancelEventHandler(this.textBoxEmpty_Validating);
27
this.textBoxOccupation.Validating += new CancelEventHandler(this.textBoxOccupation_Validating);
28
this.textBoxAge.Validating += new CancelEventHandler(this.textBoxEmpty_Validating);
29
30
//当控件中文本发生改变,就激发TextChanged事件。
31
this.textBoxName.TextChanged +=new EventHandler(textBox_TextChanged);
32
this.textBoxAddress.TextChanged+=new EventHandler(textBox_TextChanged);
33
this.textBoxOccupation.TextChanged+=new EventHandler(textBox_TextChanged);
34
this.textBoxAge.TextChanged += new EventHandler(textBox_TextChanged);
35
}
36
37
private void buttonOK_Click(object sender, EventArgs e)
38
{
39
//No testing for invalid values are made ,as that should not be necessary
40
string output;
41
42
//Concatenate the text values of for TextBoxes.
43
output = "Name:" + this.textBoxName.Text + "\r\n";
44
output += "Address:" + this.textBoxAddress.Text + "\r\n";
45
output += "Occupation:" + this.textBoxOccupation.Text + "\r\n";
46
output += "Age:" + this.textBoxAge.Text;
47
48
//Insert the new text.
49
this.textBoxOutput.Text = output;
50
}
51
52
private void buttonHelp_Click(object sender, EventArgs e)
53
{
54
//Write a short description of each TextBox in the Output TextBox.
55
string output;
56
57
output = "Name=Your name\r\n";
58
output += "Address=Your address\r\n";
59
output += "Occupation=Only allowed value is 'Programmer'\r\n";
60
output += "Age=Your age";
61
62
//Insert the new text.
63
this.textBoxOutput.Text = output;
64
}
65
66
private void textBoxEmpty_Validating(object sender, CancelEventArgs e)
67
{
68
//我们知道这个sender是一个对话框,所以我们将他们强制转换为TextBox
69
TextBox tb = (TextBox)sender;
70
71
//如果对话框是空的话我们设置TextBox背景色为红色来象征问题。
72
//如果控件有valid信息,我们就使用控件的Tag值来指出。
73
if (tb.Text.Length == 0)
74
{
75
tb.BackColor = Color.Red;
76
tb.Tag = false;
77
//在这个例子中我们不想取消further processing
78
//但是如果我们想要这么做的话,我们只需要添加以下一行:
79
//e.Cancel=true;
80
}
81
else
82
{
83
this.BackColor = SystemColors.Window;
84
tb.Tag = true;
85
}
86
//Finally ,we call ValidateOK which will set the value of the OK button.
87
ValidateOK();
88
}
89
90
private void textBoxOccupation_Validating(object sender, CancelEventArgs e)
91
{
92
//Cast the sender object to a textBox
93
TextBox tb = (TextBox)sender;
94
95
//Check if the values are correct
96
if (tb.Text.CompareTo("Programmer") == 0 || tb.Text.Length == 0)
97
{
98
tb.Tag = true;
99
tb.BackColor = Color.Red;
100
}
101
else
102
{
103
tb.Tag = false;
104
tb.BackColor = SystemColors.Window;
105
}
106
107
//Set the state of the OK button
108
ValidateOK();
109
}
110
111
private void textBoxAge_KeyPress(object sender, KeyPressEventArgs e)
112
{
113
if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8)
114
{
115
e.Handled = true; //Remove the character
116
//等于true告诉用户不应该对字符进行任何操作
117
}
118
}
119
120
private void textBox_TextChanged(object sender, EventArgs e)
121
{
122
//Cast the sender object to a TextBox
123
TextBox tb = (TextBox)sender;
124
125
//Test if the data is valid and set the tag and back ground color accordingly.
126
if (tb.Text.Length == 0 && tb != textBoxOccupation)
127
{
128
tb.Tag = false;
129
tb.BackColor = Color.Red;
130
}
131
else if (tb == textBoxOccupation && (tb.Text.Length != 0 && tb.Text.CompareTo("Programmer") != 0))
132
{
133
//Don't set the color here,as it will color change while the user is typing
134
tb.Tag = false;
135
}
136
else
137
{
138
tb.Tag = true;
139
tb.BackColor = SystemColors.Window;
140
}
141
142
//Call ValidateOK to set the OK button
143
ValidateOK();
144
}
145
146
private void ValidateOK()
147
{
148
//Set the OK button to enabled if all the Tags are true
149
this.buttonOK.Enabled = ((bool)(this.textBoxName.Tag) &&
150
(bool)(this.textBoxAge.Tag) &&
151
(bool)(this.textBoxAddress.Tag) &&
152
(bool)(this.textBoxOccupation.Tag));
153
}
154
}
155
}
using System;2
using System.Collections.Generic;3
using System.ComponentModel;4
using System.Data;5
using System.Drawing;6
using System.Text;7
using System.Windows.Forms;8

9
namespace WA_TextBoxTest10
{11
public partial class Form1 : Form12
{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
this.textBoxOccupation.Tag = false;23

24
//订阅事件Subscriptions to events25
this.textBoxName.Validating += new CancelEventHandler(this.textBoxEmpty_Validating);26
this.textBoxAddress.Validating += new CancelEventHandler(this.textBoxEmpty_Validating);27
this.textBoxOccupation.Validating += new CancelEventHandler(this.textBoxOccupation_Validating);28
this.textBoxAge.Validating += new CancelEventHandler(this.textBoxEmpty_Validating);29

30
//当控件中文本发生改变,就激发TextChanged事件。31
this.textBoxName.TextChanged +=new EventHandler(textBox_TextChanged);32
this.textBoxAddress.TextChanged+=new EventHandler(textBox_TextChanged);33
this.textBoxOccupation.TextChanged+=new EventHandler(textBox_TextChanged);34
this.textBoxAge.TextChanged += new EventHandler(textBox_TextChanged);35
}36

37
private void buttonOK_Click(object sender, EventArgs e)38
{39
//No testing for invalid values are made ,as that should not be necessary40
string output;41
42
//Concatenate the text values of for TextBoxes.43
output = "Name:" + this.textBoxName.Text + "\r\n";44
output += "Address:" + this.textBoxAddress.Text + "\r\n";45
output += "Occupation:" + this.textBoxOccupation.Text + "\r\n";46
output += "Age:" + this.textBoxAge.Text;47
48
//Insert the new text.49
this.textBoxOutput.Text = output;50
}51

52
private void buttonHelp_Click(object sender, EventArgs e)53
{54
//Write a short description of each TextBox in the Output TextBox.55
string output;56

57
output = "Name=Your name\r\n";58
output += "Address=Your address\r\n";59
output += "Occupation=Only allowed value is 'Programmer'\r\n";60
output += "Age=Your age";61

62
//Insert the new text.63
this.textBoxOutput.Text = output;64
}65

66
private void textBoxEmpty_Validating(object sender, CancelEventArgs e)67
{68
//我们知道这个sender是一个对话框,所以我们将他们强制转换为TextBox69
TextBox tb = (TextBox)sender;70

71
//如果对话框是空的话我们设置TextBox背景色为红色来象征问题。72
//如果控件有valid信息,我们就使用控件的Tag值来指出。73
if (tb.Text.Length == 0)74
{75
tb.BackColor = Color.Red;76
tb.Tag = false;77
//在这个例子中我们不想取消further processing78
//但是如果我们想要这么做的话,我们只需要添加以下一行:79
//e.Cancel=true;80
}81
else82
{83
this.BackColor = SystemColors.Window;84
tb.Tag = true;85
}86
//Finally ,we call ValidateOK which will set the value of the OK button.87
ValidateOK();88
}89

90
private void textBoxOccupation_Validating(object sender, CancelEventArgs e)91
{92
//Cast the sender object to a textBox93
TextBox tb = (TextBox)sender;94
95
//Check if the values are correct96
if (tb.Text.CompareTo("Programmer") == 0 || tb.Text.Length == 0)97
{98
tb.Tag = true;99
tb.BackColor = Color.Red;100
}101
else102
{103
tb.Tag = false;104
tb.BackColor = SystemColors.Window;105
}106

107
//Set the state of the OK button108
ValidateOK();109
}110

111
private void textBoxAge_KeyPress(object sender, KeyPressEventArgs e)112
{113
if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8)114
{115
e.Handled = true; //Remove the character116
//等于true告诉用户不应该对字符进行任何操作117
}118
}119

120
private void textBox_TextChanged(object sender, EventArgs e)121
{122
//Cast the sender object to a TextBox123
TextBox tb = (TextBox)sender;124

125
//Test if the data is valid and set the tag and back ground color accordingly.126
if (tb.Text.Length == 0 && tb != textBoxOccupation)127
{128
tb.Tag = false;129
tb.BackColor = Color.Red;130
}131
else if (tb == textBoxOccupation && (tb.Text.Length != 0 && tb.Text.CompareTo("Programmer") != 0))132
{133
//Don't set the color here,as it will color change while the user is typing134
tb.Tag = false;135
}136
else137
{138
tb.Tag = true;139
tb.BackColor = SystemColors.Window;140
}141

142
//Call ValidateOK to set the OK button143
ValidateOK();144
}145

146
private void ValidateOK()147
{ 148
//Set the OK button to enabled if all the Tags are true149
this.buttonOK.Enabled = ((bool)(this.textBoxName.Tag) &&150
(bool)(this.textBoxAge.Tag) &&151
(bool)(this.textBoxAddress.Tag) &&152
(bool)(this.textBoxOccupation.Tag));153
}154
}155
}[示例说明]:
1、开发语言:C#
2、开发环境:Visual Studio.Net 2005 Team suite
3、开发模板:C#.net项目->Windows应用程序
posted on 2006-11-27 21:52 volnet(可以叫我大V) 阅读(2894) 评论(0) 收藏 举报


浙公网安备 33010602011771号