大家知道,在C#中,TextBox控件有keypress、keyup、和keydown事件等对输入字符来控制。下面简单谈下在.net中c# textbox数字输入的实现以及代码示例。

公司主营业务:成都网站制作、成都网站设计、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。成都创新互联是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。成都创新互联推出江陵免费做网站回馈大家。
当界面上要用户输入只有数字的字符时,默认的c# textbox数字是不能胜任的,网上有很多网友们提供了很多的做法,我总结了一下写了一个在C#下的实现,做到了如下的几点:
1:只能输入类似这样的字符:-123456.789;1234.789;
2:在输入的字符串中不能存在两个点符:12456.78//正确;12.456.78//不正确;
3:如果表示负数可以在字符串的最前面加一个减号“-”,也只能加到弟一个字符的位置;
4:可以用复制粘帖功能和菜单功能,但是只对能正确格式的字符串有效,比如:12.34可以,Abc不可以;
5:只是得到一个字符串,还可以在这个基础上再改进自己所需的,经如添加对十六进制的支持等。
代码如下在.NET下用C#写的:
- using System;
 - using System.Windows.Forms;
 - namespace NumTextBox
 - {
 - ///
 - /// NumTextBox 的摘要说明。
 - ///
 - public class TextBoxNumEx:System.Windows.Forms.TextBox
 - {
 - public const int WM_CONTEXTMENU = 0x007b;//右键菜单消息
 - public const int WM_CHAR = 0x0102; //输入字符消息(键盘输入的,输入法输入的好像不是这个消息)
 - public const int WM_CUT = 0x0300; //程序发送此消息给一个编辑框或combobox来删除当前选择的文本
 - public const int WM_COPY = 0x0301; //程序发送此消息给一个编辑框或combobox来复制当前选择的文本到剪贴板
 - public const int WM_PASTE = 0x0302; //程序发送此消息给editcontrol或combobox从剪贴板中得到数据
 - public const int WM_CLEAR = 0x0303; //程序发送此消息给editcontrol或combobox清除当前选择的内容;
 - public const int WM_UNDO = 0x0304; //程序发送此消息给editcontrol或combobox撤消***一次操作
 - public TextBoxNumEx()
 - {
 - //
 - // TODO: 在此处添加构造函数逻辑
 - //
 - }
 - protected override void WndProc(ref Message m)
 - {
 - switch(m.Msg)
 - {
 - case WM_CHAR:
 - System.Console.WriteLine(m.WParam);
 - bool isSign = ((int)m.WParam == 45);
 - bool isNum = ((int)m.WParam >= 48) && ((int)m.WParam <= 57);
 - bool isBack = (int)m.WParam == (int)Keys.Back;
 - bool isDelete = (int)m.WParam == (int)Keys.Delete;//实际上这是一个"."键
 - bool isCtr = ((int)m.WParam == 24) || ((int)m.WParam == 22) || ((int)m.WParam == 26) ||((int)m.WParam == 3);
 - if( isNum || isBack || isCtr)
 - {
 - base.WndProc (ref m);
 - }
 - if (isSign)
 - {
 - if (this.SelectionStart!=0)
 - {
 - break;
 - }
 - base.WndProc (ref m);
 - break;
 - }
 - if (isDelete)
 - {
 - if (this.Text.IndexOf(".")<0)
 - {
 - base.WndProc (ref m);
 - }
 - }
 - if ((int)m.WParam == 1)
 - {
 - this.SelectAll();
 - }
 - break;
 - case WM_PASTE:
 - IDataObject iData = Clipboard.GetDataObject();//取剪贴板对象
 - if(iData.GetDataPresent(DataFormats.Text)) //判断是否是Text
 - {
 - string str = (string)iData.GetData(DataFormats.Text);//取数据
 - if (MatchNumber(str))
 - {
 - base.WndProc (ref m);
 - break;
 - }
 - }
 - m.Result = (IntPtr)0;//不可以粘贴
 - break;
 - default:
 - base.WndProc (ref m);
 - break;
 - }
 - }
 - private bool MatchNumber(string ClipboardText)
 - {
 - int index=0;
 - string strNum = "-0.123456789";
 - index = ClipboardText.IndexOf(strNum[0]);
 - if (index>=0)
 - {
 - if (index>0)
 - {
 - return false;
 - }
 - index = this.SelectionStart;
 - if (index>0)
 - {
 - return false;
 - }
 - }
 - index = ClipboardText.IndexOf(strNum[2]);
 - if (index!=-1)
 - {
 - index = this.Text.IndexOf(strNum[2]);
 - if (index!=-1)
 - {
 - return false;
 - }
 - }
 - for(int i=0; i
 - {
 - index = strNum.IndexOf(ClipboardText[i]);
 - if (index <0)
 - {
 - return false;
 - }
 - }
 - return true;
 - }
 - }
 - }
 
本文来自:编程论坛 作者:佚名
【编辑推荐】
                当前名称:.net控制技巧:c#textbox数字的输入
                
                网页路径:http://www.csdahua.cn/qtweb/news39/210689.html
            
网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网