本站首页    管理页面    写新日志    退出


«August 2025»
12
3456789
10111213141516
17181920212223
24252627282930
31


公告
暂无公告...

我的分类(专题)

日志更新

最新评论

留言板

链接


Blog信息
blog名称:
日志总数:18
评论数量:10
留言数量:1
访问次数:137828
建立时间:2005年11月22日




[编程]如何让TextBox只显示数字
电脑与网络

flymcx 发表于 2006/6/16 10:43:35

TextBox本身不支持对输入的数字控制,因为我们只有自己写。 我就这里写了一个。  public class NT_TextBox : TextBox {  public enum NumericType  {   None,   Int,   Float  }  private NumericType _NumericOpration;  public NumericType NumericOpration  {   get { return _NumericOpration; }   set { _NumericOpration = value; }  }   protected override void WndProc(ref Message m)  {   if(NumericType.None==NumericOpration)   {    base.WndProc(ref m);    return;   }   int WM_CHAR = 0x0102;   if(m.Msg == WM_CHAR)   {    char e=(char)m.WParam;    if(DealInputChar(e))     base.WndProc(ref m);   }   else   {    base.WndProc (ref m);   }  }   private bool DealInputChar(char e)  {   int code=(int)e;   int min=(char)'0';   int max=(char)'9';   if(code==(int)Keys.Back)    return true;   if(code==(int)Keys.Left)    return true;   if(code==(int)Keys.Right)    return true;   if(code==(int)'.')   {    if(Text.Trim().IndexOf(".")==-1)    {     if(NumericOpration==NumericType.Float)      return true;     else      return false;    }    else     return false;   }   if(code==(int)'-')   {    if(Text.Trim().IndexOf("-")==-1)    {     if(SelectionStart==0)      return true;     else      return false;    }    else     return false;   }   if(code<min||code>max)    return false;   return true;  } }


阅读全文(2886) | 回复(0) | 编辑 | 精华
 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.047 second(s), page refreshed 144754500 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号