热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

VS2003和VS2005下文本框的只能输入数字类型的区别

前几天在做项目时,分别用03和05做,发现在判别文本框检验只能输入数字类型的数字时,在TextBox_KeyPress事件中e.keychar的属性有所区别,在VS2003下,是只
前几天在做项目时,分别用03和05做,发现在判别文本框检验只能输入数字类型的数字时,在TextBox_KeyPress事件中e.keychar的属性有所区别,在VS2003下,是只读的,而在VS2005下是既可读又可写.所以在输入的数字是全角数字时,所处理的方法有所区别.现在把两种方法写下来:
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下:          
 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                defaultbreak;
65            }

66
67        }

68        #endregion
          相对来说,2005下,实现起来更容易一些.

转:https://www.cnblogs.com/yangjie5188/archive/2008/02/16/1070375.html



推荐阅读
author-avatar
xiaojin
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有