VS2003和VS2005下文本框的只能输入数字类型的区别
前几天在做项目时,分别用03和05做,发现在判别文本框检验只能输入数字类型的数字时,在TextBox_KeyPress事件中e.keychar的属性有所区别,在VS2003下,是只读的,而在VS2005下是既可读又可写.所以在输入的数字是全角数字时,所处理的方法有所区别.现在把两种方法写下来:
VS2003下:
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 = 0; i < c.Length; i++)
14 {
15 if (c[i] == 12288)
16 {
17 c[i] = (char)32;
18 continue;
19 }
20 if (c[i] > 65280 && c[i] < 65375)
21 c[i] = (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 = textBox2.Text.Trim();
29 if (!char.IsDigit(e.KeyChar))
30 {
31 e.Handled = true;
32 textBox2.Text = ToDBC(old);
33 }
34 //'\b'是退格键值
35 if (e.KeyChar == '\b' || e.KeyChar == '.')
36 {
37 e.Handled = false;
38 }
39 }
40 #endregion
VS2005下: 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 = 0; i < c.Length; i++)
14 {
15 if (c[i] == 12288)
16 {
17 c[i] = (char)32;
18 continue;
19 }
20 if (c[i] > 65280 && c[i] < 65375)
21 c[i] = (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 = textBox2.Text.Trim();
29 if (!char.IsDigit(e.KeyChar))
30 {
31 e.Handled = true;
32 textBox2.Text = ToDBC(old);
33 }
34 //'\b'是退格键值
35 if (e.KeyChar == '\b' || e.KeyChar == '.')
36 {
37 e.Handled = 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 = true;
9 }
10 //'\b'是退格键值
11 if (e.KeyChar == '\b') e.Handled = false;
12 switch (e.KeyChar)//处理全角字
13 {
14 case '1':
15 {
16 e.KeyChar = '1';
17 break;
18 }
19 case '2':
20 {
21 e.KeyChar = '2';
22 break;
23 }
24 case '3':
25 {
26 e.KeyChar = '3';
27 break;
28 }
29 case '4':
30 {
31 e.KeyChar = '4';
32 break;
33 }
34 case '5':
35 {
36 e.KeyChar = '5';
37 break;
38 }
39 case '6':
40 {
41 e.KeyChar = '6';
42 break;
43 }
44 case '7':
45 {
46 e.KeyChar = '7';
47 break;
48 }
49 case '8':
50 {
51 e.KeyChar = '8';
52 break;
53 }
54 case '9':
55 {
56 e.KeyChar = '9';
57 break;
58 }
59 case '0':
60 {
61 e.KeyChar = '0';
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 = true;
9 }
10 //'\b'是退格键值
11 if (e.KeyChar == '\b') e.Handled = false;
12 switch (e.KeyChar)//处理全角字
13 {
14 case '1':
15 {
16 e.KeyChar = '1';
17 break;
18 }
19 case '2':
20 {
21 e.KeyChar = '2';
22 break;
23 }
24 case '3':
25 {
26 e.KeyChar = '3';
27 break;
28 }
29 case '4':
30 {
31 e.KeyChar = '4';
32 break;
33 }
34 case '5':
35 {
36 e.KeyChar = '5';
37 break;
38 }
39 case '6':
40 {
41 e.KeyChar = '6';
42 break;
43 }
44 case '7':
45 {
46 e.KeyChar = '7';
47 break;
48 }
49 case '8':
50 {
51 e.KeyChar = '8';
52 break;
53 }
54 case '9':
55 {
56 e.KeyChar = '9';
57 break;
58 }
59 case '0':
60 {
61 e.KeyChar = '0';
62 break;
63 }
64 default: break;
65 }
66
67 }
68 #endregion