[示例涉及]:
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 窗体设计器生成的代码#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.textBoxOccupation = new System.Windows.Forms.TextBox();
36
this.textBoxAge = new System.Windows.Forms.TextBox();
37
this.textBoxOutput = new System.Windows.Forms.TextBox();
38
this.label3 = new System.Windows.Forms.Label();
39
this.label4 = new System.Windows.Forms.Label();
40
this.label5 = new System.Windows.Forms.Label();
41
this.buttonOK = new System.Windows.Forms.Button();
42
this.buttonHelp = new System.Windows.Forms.Button();
43
this.SuspendLayout();
44
//
45
// label1
46
//
47
this.label1.AutoSize = true;
48
this.label1.Location = new System.Drawing.Point(26, 27);
49
this.label1.Name = "label1";
50
this.label1.Size = new System.Drawing.Size(59, 12);
51
this.label1.TabIndex = 0;
52
this.label1.Text = "labelName";
53
//
54
// label2
55
//
56
this.label2.AutoSize = true;
57
this.label2.Location = new System.Drawing.Point(26, 53);
58
this.label2.Name = "label2";
59
this.label2.Size = new System.Drawing.Size(77, 12);
60
this.label2.TabIndex = 1;
61
this.label2.Text = "labelAddress";
62
//
63
// textBoxName
64
//
65
this.textBoxName.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
66
| System.Windows.Forms.AnchorStyles.Right)));
67
this.textBoxName.Location = new System.Drawing.Point(127, 24);
68
this.textBoxName.Name = "textBoxName";
69
this.textBoxName.Size = new System.Drawing.Size(317, 21);
70
this.textBoxName.TabIndex = 2;
71
//
72
// textBoxAddress
73
//
74
this.textBoxAddress.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
75
| System.Windows.Forms.AnchorStyles.Right)));
76
this.textBoxAddress.Location = new System.Drawing.Point(127, 50);
77
this.textBoxAddress.Multiline = true;
78
this.textBoxAddress.Name = "textBoxAddress";
79
this.textBoxAddress.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
80
this.textBoxAddress.Size = new System.Drawing.Size(317, 102);
81
this.textBoxAddress.TabIndex = 2;
82
//
83
// textBoxOccupation
84
//
85
this.textBoxOccupation.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
86
| System.Windows.Forms.AnchorStyles.Right)));
87
this.textBoxOccupation.Location = new System.Drawing.Point(127, 158);
88
this.textBoxOccupation.Name = "textBoxOccupation";
89
this.textBoxOccupation.Size = new System.Drawing.Size(317, 21);
90
this.textBoxOccupation.TabIndex = 3;
91
this.textBoxOccupation.Validating += new System.ComponentModel.CancelEventHandler(this.textBoxOccupation_Validating);
92
//
93
// textBoxAge
94
//
95
this.textBoxAge.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
96
| System.Windows.Forms.AnchorStyles.Right)));
97
this.textBoxAge.Location = new System.Drawing.Point(127, 193);
98
this.textBoxAge.Name = "textBoxAge";
99
this.textBoxAge.Size = new System.Drawing.Size(99, 21);
100
this.textBoxAge.TabIndex = 4;
101
this.textBoxAge.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBoxAge_KeyPress);
102
//
103
// textBoxOutput
104
//
105
this.textBoxOutput.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
106
| System.Windows.Forms.AnchorStyles.Left)
107
| System.Windows.Forms.AnchorStyles.Right)));
108
this.textBoxOutput.Location = new System.Drawing.Point(28, 243);
109
this.textBoxOutput.Multiline = true;
110
this.textBoxOutput.Name = "textBoxOutput";
111
this.textBoxOutput.ReadOnly = true;
112
this.textBoxOutput.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
113
this.textBoxOutput.Size = new System.Drawing.Size(416, 150);
114
this.textBoxOutput.TabIndex = 5;
115
//
116
// label3
117
//
118
this.label3.AutoSize = true;
119
this.label3.Location = new System.Drawing.Point(26, 161);
120
this.label3.Name = "label3";
121
this.label3.Size = new System.Drawing.Size(95, 12);
122
this.label3.TabIndex = 6;
123
this.label3.Text = "labelOccupation";
124
//
125
// label4
126
//
127
this.label4.AutoSize = true;
128
this.label4.Location = new System.Drawing.Point(26, 196);
129
this.label4.Name = "label4";
130
this.label4.Size = new System.Drawing.Size(53, 12);
131
this.label4.TabIndex = 7;
132
this.label4.Text = "labelAge";
133
//
134
// label5
135
//
136
this.label5.AutoSize = true;
137
this.label5.Location = new System.Drawing.Point(26, 228);
138
this.label5.Name = "label5";
139
this.label5.Size = new System.Drawing.Size(71, 12);
140
this.label5.TabIndex = 8;
141
this.label5.Text = "labelOutput";
142
//
143
// buttonOK
144
//
145
this.buttonOK.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
146
this.buttonOK.Location = new System.Drawing.Point(458, 18);
147
this.buttonOK.Name = "buttonOK";
148
this.buttonOK.Size = new System.Drawing.Size(82, 21);
149
this.buttonOK.TabIndex = 9;
150
this.buttonOK.Text = "OK";
151
this.buttonOK.UseVisualStyleBackColor = true;
152
this.buttonOK.Click += new System.EventHandler(this.buttonOK_Click);
153
//
154
// buttonHelp
155
//
156
this.buttonHelp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
157
this.buttonHelp.CausesValidation = false;
158
this.buttonHelp.Location = new System.Drawing.Point(458, 43);
159
this.buttonHelp.Name = "buttonHelp";
160
this.buttonHelp.Size = new System.Drawing.Size(82, 21);
161
this.buttonHelp.TabIndex = 10;
162
this.buttonHelp.Text = "Help";
163
this.buttonHelp.UseVisualStyleBackColor = true;
164
this.buttonHelp.Click += new System.EventHandler(this.buttonHelp_Click);
165
//
166
// Form1
167
//
168
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
169
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
170
this.ClientSize = new System.Drawing.Size(561, 405);
171
this.Controls.Add(this.buttonHelp);
172
this.Controls.Add(this.buttonOK);
173
this.Controls.Add(this.label5);
174
this.Controls.Add(this.label4);
175
this.Controls.Add(this.label3);
176
this.Controls.Add(this.textBoxOutput);
177
this.Controls.Add(this.textBoxAge);
178
this.Controls.Add(this.textBoxOccupation);
179
this.Controls.Add(this.textBoxAddress);
180
this.Controls.Add(this.textBoxName);
181
this.Controls.Add(this.label2);
182
this.Controls.Add(this.label1);
183
this.Name = "Form1";
184
this.Text = "Form1";
185
this.Validating += new System.ComponentModel.CancelEventHandler(this.textBoxEmpty_Validating);
186
this.ResumeLayout(false);
187
this.PerformLayout();
188
189
}
190
191
#endregion
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

2



3

4



5


6

7

8

9

10


11

12

13

14

15



16

17



18

19

20

21

22

23


24

25


26

27

28

29

30



31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

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
}

2

3

4

5

6

7

8

9

10



11

12



13

14



15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38



39

40

41

42

43

44

45

46

47

48

49

50

51

52

53



54

55

56

57

58

59

60

61

62

63

64

65

66

67



68

69

70

71

72

73

74



75

76

77

78

79

80

81

82



83

84

85

86

87

88

89

90

91



92

93

94

95

96

97



98

99

100

101

102



103

104

105

106

107

108

109

110

111

112



113

114



115

116

117

118

119

120

121



122

123

124

125

126

127



128

129

130

131

132



133

134

135

136

137



138

139

140

141

142

143

144

145

146

147



148

149

150

151

152

153

154

155

[示例说明]:
1、开发语言:C#
2、开发环境:Visual Studio.Net 2005 Team suite
3、开发模板:C#.net项目->Windows应用程序