您的位置:程序门 -> .net技术 -> c#



桌面程序中文本框怎么限制只能输入数字?


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


桌面程序中文本框怎么限制只能输入数字?
发表于:2007-05-14 22:00:29 楼主
我的桌面程序里   希望其中的文本框只能输入数字,请问能不能像web程序里那样,设置正则表达式,如果能设置该怎么设置呢?

我现在的方法是:
    private   void   textbox3_keypress(object   sender,   keypresseventargs   e)
                {
                        if   ((e.keychar   <   '0 '   ¦ ¦   e.keychar   >   '9 ')   &&   e.keychar   !=   (char)13   &&   e.keychar   !=   (char)8)
                        {
                                e.handled   =   true;
                        }
                }
但是发现,在半角状态下是正常的,在全角状态下不能输入任何字符了,请问哪位高手知道这是怎么回事,能不能解决呢?
谢谢先   !
发表于:2007-05-14 22:05:091楼 得分:0
try{int   i=convert.toint32(textbox1.text.trim());}
catch(exception   e)
{
  messagebox.show( "输入的不是数字! ");
  textbox1.text= " ";
}
发表于:2007-05-14 22:16:552楼 得分:0
using   system.runtime.interopservices;

[dllimport( "user32.dll ")]
static   extern   int   setwindowlong(intptr   hwnd,   int   nindex,   int   dwnewlong);
[dllimport( "user32.dll ")]
static   extern   int   getwindowlong(intptr   hwnd,   int   nindex);
public   const   int   gwl_style   =   -16;
public   const   int   es_number   =   0x2000;
private   void   form1_load(object   sender,   eventargs   e)
{
        setwindowlong(textbox1.handle,   gwl_style,
                getwindowlong(textbox1.handle,   gwl_style)   ¦   es_number);
}
发表于:2007-05-14 22:20:293楼 得分:0
private   void   textbox1_keypress(object   sender,   keypresseventargs   e)
{
        e.handled   =   ( "0123456789 "   +   (char)8   +   (char)13).indexof(e.keychar)   <   0;
}

这样现在不了粘贴
除非在textchange事件中再处理一下
发表于:2007-05-14 22:20:404楼 得分:0
限制
发表于:2007-05-14 22:42:275楼 得分:0
可以用ls的方法过滤...
发表于:2007-05-14 22:48:296楼 得分:0
private   void   text1_keypress(object   sender,   keypresseventargs   e)
                {
                        if   ((short)e.keychar   > =   48   &&   (short)e.keychar   <=   57   ¦ ¦   (short)e.keychar   ==   8)
                        {
                                e.handled   =   false;
                        }
                        else
                        {
                                e.handled   =   true;
                        }
                }
发表于:2007-05-14 23:16:367楼 得分:0
晕,没那么复杂吧,
两个简单的方法:
1:使用editemask空间,设置为只能输入数字
2:用char.isnumber()函数来判读输入的每个字符是不是数字就行了。
 
ok!
发表于:2007-05-14 23:27:558楼 得分:0
楼主的意思是全角状态下的数字也允许输入?如果是,这样试下

private   void   textbox1_textchanged(object   sender,   eventargs   e)
{
          textbox1.text   =   regex.replace(textbox1.text,   @ "[^0-90-9] ",   " ");
}

否则这样试下

private   void   textbox1_textchanged(object   sender,   eventargs   e)
{
          textbox1.text   =   regex.replace(textbox1.text,   @ "[^0-9] ",   " ");
}
发表于:2007-05-15 00:11:539楼 得分:0
好像楼上的最确切了,最近刚看正则表达式
发表于:2007-05-15 08:43:4810楼 得分:0
lxcnn(过客)   (   )   信誉:100         blog       加为好友  

你写的那一个是不是要引用别的类
我试的好像不行啊

asp我知道可以   winform真的可以吗
发表于:2007-05-15 08:57:2711楼 得分:0
mark
发表于:2007-05-15 09:26:4412楼 得分:0
可以用正则表达式控制

using   system.text.regularexpressions;

private   void   button1_click(object   sender,   system.eventargs   e)
{
      string   regstr   =   @ "^-?\d+$ ";//整数,包括全角
      regex   regex   =   new   regex(regstr);
      string   text   =   textbox1.text;
      if   (regex.ismatch(text))
      {
messagebox.show( "ok ");
      }
      else
      {
messagebox.show( "only   can   input   number ");
      }
}
发表于:2007-05-15 09:26:5213楼 得分:0
to:yudi010

textbox1.text   =   system.text.regularexpressions.regex.replace(textbox1.text,   @ "[^0-90-9] ",   " ");

ps:事实上,这样做代码比较简洁,也能达到效果,但效率不高
发表于:2007-05-15 09:30:1914楼 得分:0
方法   1。try{int   i=convert.toint32(textbox1.text.trim());}
          catch(exception   e)
          {
                messagebox.show( "输入的不是数字! ");
                textbox1.text= " ";
          }
方法2。

        用正则表达式
发表于:2007-05-15 09:44:4715楼 得分:0
lxcnn(过客)   (   )   信誉:100         blog       加为好友  
ok
多谢了   哈
不过有一个缺点,就是输入错误的焦点在顶部
发表于:2007-05-15 09:52:1116楼 得分:0
to:yudi010

那就在后面再加一行
textbox1.selectionstart   =   textbox1.text.length;

这个是指定到尾部,当然,如果是在中间插入的话,还是有缺点,虽然也可以做到,但是没那个必要的
发表于:2007-05-15 09:57:3717楼 得分:0
to   lxcnn(过客)
明白了   谢谢了
看来自己的基础真的很差   哈哈
发表于:2007-05-15 10:01:2618楼 得分:0
自己写个textbox控件,加个属性
using   system;
using   system.collections.generic;
using   system.text;
using   system.windows.forms;

namespace   operatesql
{
        class   mytextbox:textbox
        {
                private   bool   _isnum   =   false;

                public   bool   isnum
                {
                        get   {   return   _isnum;   }
                        set   {   _isnum   =   value;   }
                }

                protected   override   void   onkeypress(keypresseventargs   e)
                {
                        if   (_isnum)
                        {
                                if   (e.keychar   <   '0 '   ¦ ¦   e.keychar   >   '9 ')
                                {
                                        e.handled   =   true;
                                }
                                else
                                {
                                        base.onkeypress(e);
                                }
                        }
                }
        }
}

用的时候把isnum属性设为true就行了


快速检索

最新资讯
热门点击