.net控制技巧:c# textbox数字的输入

开发 后端
在.net控制TextBox中只能输入数字网友可能有很多种方法实现,本文章主要介绍的是.net中c# textbox数字的输入,希望能给大家一些帮助。

 大家知道,在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#写的:

  1. using System;  
  2. using System.Windows.Forms;  
  3.  
  4. namespace NumTextBox  
  5. {  
  6.  ///   
  7.  /// NumTextBox 的摘要说明。  
  8.  /// 
  9.  
  10.  public class TextBoxNumEx:System.Windows.Forms.TextBox  
  11.  {  
  12.   public const int WM_CONTEXTMENU = 0x007b;//右键菜单消息   
  13.   public const int WM_CHAR = 0x0102;       //输入字符消息(键盘输入的,输入法输入的好像不是这个消息)  
  14.   public const int WM_CUT = 0x0300;        //程序发送此消息给一个编辑框或combobox来删除当前选择的文本  
  15.   public const int WM_COPY = 0x0301;       //程序发送此消息给一个编辑框或combobox来复制当前选择的文本到剪贴板  
  16.   public const int WM_PASTE = 0x0302;      //程序发送此消息给editcontrol或combobox从剪贴板中得到数据  
  17.   public const int WM_CLEAR = 0x0303;      //程序发送此消息给editcontrol或combobox清除当前选择的内容;  
  18.   public const int WM_UNDO = 0x0304;        //程序发送此消息给editcontrol或combobox撤消***一次操作  
  19.  
  20.   public TextBoxNumEx()  
  21.   {  
  22.    //  
  23.    // TODO: 在此处添加构造函数逻辑  
  24.    //  
  25.   }  
  26.   protected override void WndProc(ref Message m)   
  27.   {   
  28.    switch(m.Msg)  
  29.    {   
  30.     case WM_CHAR:  
  31.      System.Console.WriteLine(m.WParam);  
  32.      bool isSign = ((int)m.WParam == 45);  
  33.      bool isNum = ((int)m.WParam >= 48) && ((int)m.WParam <= 57);  
  34.      bool isBack = (int)m.WParam == (int)Keys.Back;  
  35.      bool isDelete = (int)m.WParam == (int)Keys.Delete;//实际上这是一个"."键  
  36.      bool isCtr = ((int)m.WParam == 24) || ((int)m.WParam == 22) || ((int)m.WParam == 26) ||((int)m.WParam == 3);  
  37.  
  38.      if( isNum || isBack || isCtr)  
  39.      {  
  40.       base.WndProc (ref m);  
  41.      }  
  42.      if (isSign)  
  43.      {  
  44.       if (this.SelectionStart!=0)  
  45.       {  
  46.        break;  
  47.       }  
  48.       base.WndProc (ref m);  
  49.       break;  
  50.      }  
  51.      if (isDelete)  
  52.      {  
  53.       if (this.Text.IndexOf(".")<0)  
  54.       {  
  55.        base.WndProc (ref m);  
  56.       }  
  57.      }  
  58.      if ((int)m.WParam == 1)  
  59.      {  
  60.       this.SelectAll();  
  61.      }  
  62.      break;  
  63.     case WM_PASTE:  
  64.      IDataObject iData = Clipboard.GetDataObject();//取剪贴板对象  
  65.        
  66.      if(iData.GetDataPresent(DataFormats.Text)) //判断是否是Text  
  67.      {  
  68.       string str = (string)iData.GetData(DataFormats.Text);//取数据  
  69.       if (MatchNumber(str))   
  70.       {  
  71.        base.WndProc (ref m);  
  72.        break;  
  73.       }  
  74.      }  
  75.      m.Result = (IntPtr)0;//不可以粘贴  
  76.      break;  
  77.     default:  
  78.      base.WndProc (ref m);  
  79.      break;  
  80.    }  
  81.   }  
  82.   private bool MatchNumber(string ClipboardText)  
  83.   {  
  84.    int index=0;  
  85.    string strNum = "-0.123456789";  
  86.    index = ClipboardText.IndexOf(strNum[0]);  
  87.    if (index>=0)  
  88.    {  
  89.     if (index>0)  
  90.     {  
  91.      return false;  
  92.     }  
  93.     index = this.SelectionStart;  
  94.     if (index>0)  
  95.     {  
  96.      return false;  
  97.     }  
  98.    }  
  99.  
  100.    index = ClipboardText.IndexOf(strNum[2]);  
  101.    if (index!=-1)  
  102.    {  
  103.     index = this.Text.IndexOf(strNum[2]);  
  104.     if (index!=-1)  
  105.     {  
  106.      return false;  
  107.     }  
  108.    }  
  109.  
  110.    for(int i=0; i
  111.    {  
  112.     index = strNum.IndexOf(ClipboardText[i]);  
  113.     if (index <0)  
  114.     {  
  115.      return false;  
  116.     }  
  117.    }  
  118.    return true;  
  119.   }  
  120.  }  

本文来自:编程论坛   作者:佚名

【编辑推荐】

  1. C#HTTP文件传输的实现浅析
  2. 编程语言排行榜:C语言当选08年年度编程语言
  3. C++标准委员会确定将concepts特性从C++0x中移除
  4. C#3.0新特性的介绍(自动属性)
  5. C#interface定义及使用浅析
责任编辑:林琳 来源: 编程论坛
相关推荐

2009-09-08 22:53:39

c# textbox数

2009-09-10 10:22:05

C# TextBox

2009-09-09 22:31:21

c# textbox失

2009-09-09 13:31:15

C# TextBox

2009-09-02 17:29:10

C# TextBox换

2009-09-09 13:18:10

C# TextBox滚C# TextBox

2009-09-10 09:10:17

C# TextBox换

2009-08-31 16:29:21

C#控制输入法

2009-09-09 21:56:29

2009-09-09 11:29:32

C# TextBox事

2009-09-09 12:55:59

C# TextBox事

2009-09-10 09:42:53

C# TextBox

2009-10-26 09:50:11

C#与VB.NET

2009-09-08 23:35:12

c# textbox失

2009-08-27 16:54:59

C#开发技巧

2009-08-11 15:44:05

C#基本技巧

2020-07-15 14:51:39

代码C+开发

2009-08-12 14:01:17

C# Excel编程技

2009-08-26 13:15:38

C#选择控制

2009-09-01 16:29:03

QuickSort C
点赞
收藏

51CTO技术栈公众号