VS2003下:
1Code#region Code
2 //只能输入数字类型
3 private void textBox2_KeyUp(object sender, KeyEventArgs e)
4 {
5 string old = textBox2.Text.Trim();
6 textBox2.Text = ToDBC(old);
7
8 }
9 //全角转半角
10 public string ToDBC(string input)
11 {
12 char[] c = input.ToCharArray();
13 for (int i &#61; 0; i < c.Length; i&#43;&#43;)
14 {
15 if (c[i] &#61;&#61; 12288)
16 {
17 c[i] &#61; (char)32;
18 continue;
19 }
20 if (c[i] > 65280 && c[i] < 65375)
21 c[i] &#61; (char)(c[i] - 65248);
22 }
23 return new string(c);
24 }
25 //键盘按键事件
26 private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
27 {
28 string old &#61; textBox2.Text.Trim();
29 if (!char.IsDigit(e.KeyChar))
30 {
31 e.Handled &#61; true;
32 textBox2.Text &#61; ToDBC(old);
33 }
34 //&#39;\b&#39;是退格键值
35 if (e.KeyChar &#61;&#61; &#39;\b&#39; || e.KeyChar &#61;&#61; &#39;.&#39;)
36 {
37 e.Handled &#61; false;
38 }
39 }
40 #endregion
VS2005下: 2 //只能输入数字类型
3 private void textBox2_KeyUp(object sender, KeyEventArgs e)
4 {
5 string old &#61; textBox2.Text.Trim();
6 textBox2.Text &#61; ToDBC(old);
7
8 }
9 //全角转半角
10 public string ToDBC(string input)
11 {
12 char[] c &#61; input.ToCharArray();
13 for (int i &#61; 0; i < c.Length; i&#43;&#43;)
14 {
15 if (c[i] &#61;&#61; 12288)
16 {
17 c[i] &#61; (char)32;
18 continue;
19 }
20 if (c[i] > 65280 && c[i] < 65375)
21 c[i] &#61; (char)(c[i] - 65248);
22 }
23 return new string(c);
24 }
25 //键盘按键事件
26 private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
27 {
28 string old &#61; textBox2.Text.Trim();
29 if (!char.IsDigit(e.KeyChar))
30 {
31 e.Handled &#61; true;
32 textBox2.Text &#61; ToDBC(old);
33 }
34 //&#39;\b&#39;是退格键值
35 if (e.KeyChar &#61;&#61; &#39;\b&#39; || e.KeyChar &#61;&#61; &#39;.&#39;)
36 {
37 e.Handled &#61; false;
38 }
39 }
40 #endregion
1Code#region Code
2 //只能输入数字
3 private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
4 {
5
6 if (!char.IsDigit(e.KeyChar))
7 {
8 e.Handled &#61; true;
9 }
10 //&#39;\b&#39;是退格键值
11 if (e.KeyChar &#61;&#61; &#39;\b&#39;) e.Handled &#61; false;
12 switch (e.KeyChar)//处理全角字
13 {
14 case &#39;&#xff11;&#39;:
15 {
16 e.KeyChar &#61; &#39;1&#39;;
17 break;
18 }
19 case &#39;&#xff12;&#39;:
20 {
21 e.KeyChar &#61; &#39;2&#39;;
22 break;
23 }
24 case &#39;&#xff13;&#39;:
25 {
26 e.KeyChar &#61; &#39;3&#39;;
27 break;
28 }
29 case &#39;&#xff14;&#39;:
30 {
31 e.KeyChar &#61; &#39;4&#39;;
32 break;
33 }
34 case &#39;&#xff15;&#39;:
35 {
36 e.KeyChar &#61; &#39;5&#39;;
37 break;
38 }
39 case &#39;&#xff16;&#39;:
40 {
41 e.KeyChar &#61; &#39;6&#39;;
42 break;
43 }
44 case &#39;&#xff17;&#39;:
45 {
46 e.KeyChar &#61; &#39;7&#39;;
47 break;
48 }
49 case &#39;&#xff18;&#39;:
50 {
51 e.KeyChar &#61; &#39;8&#39;;
52 break;
53 }
54 case &#39;&#xff19;&#39;:
55 {
56 e.KeyChar &#61; &#39;9&#39;;
57 break;
58 }
59 case &#39;&#xff10;&#39;:
60 {
61 e.KeyChar &#61; &#39;0&#39;;
62 break;
63 }
64 default: break;
65 }
66
67 }
68 #endregion
相对来说,2005下,实现起来更容易一些.2 //只能输入数字
3 private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
4 {
5
6 if (!char.IsDigit(e.KeyChar))
7 {
8 e.Handled &#61; true;
9 }
10 //&#39;\b&#39;是退格键值
11 if (e.KeyChar &#61;&#61; &#39;\b&#39;) e.Handled &#61; false;
12 switch (e.KeyChar)//处理全角字
13 {
14 case &#39;&#xff11;&#39;:
15 {
16 e.KeyChar &#61; &#39;1&#39;;
17 break;
18 }
19 case &#39;&#xff12;&#39;:
20 {
21 e.KeyChar &#61; &#39;2&#39;;
22 break;
23 }
24 case &#39;&#xff13;&#39;:
25 {
26 e.KeyChar &#61; &#39;3&#39;;
27 break;
28 }
29 case &#39;&#xff14;&#39;:
30 {
31 e.KeyChar &#61; &#39;4&#39;;
32 break;
33 }
34 case &#39;&#xff15;&#39;:
35 {
36 e.KeyChar &#61; &#39;5&#39;;
37 break;
38 }
39 case &#39;&#xff16;&#39;:
40 {
41 e.KeyChar &#61; &#39;6&#39;;
42 break;
43 }
44 case &#39;&#xff17;&#39;:
45 {
46 e.KeyChar &#61; &#39;7&#39;;
47 break;
48 }
49 case &#39;&#xff18;&#39;:
50 {
51 e.KeyChar &#61; &#39;8&#39;;
52 break;
53 }
54 case &#39;&#xff19;&#39;:
55 {
56 e.KeyChar &#61; &#39;9&#39;;
57 break;
58 }
59 case &#39;&#xff10;&#39;:
60 {
61 e.KeyChar &#61; &#39;0&#39;;
62 break;
63 }
64 default: break;
65 }
66
67 }
68 #endregion