写了1个小函数,可以限制文本框的输入,基本上是任意字符了.但是有点小问题啊        public static char  TextLimit(char Keyin, string ListString, bool EditBasp)
        {
            string TextLimt;//要限制的字符串
            char KeyOut;//返回值
            if (EditBasp == true)//可以退格
            {
                TextLimt = ListString + (Char)8;
            }
            else
            {
                TextLimt = ListString.ToUpper(); 
            }
            int i;
            i = TextLimt.IndexOf(Keyin);
            if (i > 0)
            { KeyOut = Keyin; }
            else
            { 
//这里不知道要怎么处理了,就想如果是限制的字符,就不做任何事情
                //KeyOut ='' ;  这个写法错误
            }
            return KeyOut;
        }//这是文本框的键盘事件        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.KeyChar =TextLimit(e.KeyChar , "0123456789", true    );
        }问题就是,如果我限制  "0123456789", 只能输入1-9 ,那个0也变成无法输入了

解决方案 »

  1.   

    if (i > 0)
                { KeyOut = Keyin; }
                else
    ============
    if(i>=0)
    ...
      

  2.   

    还不是很明白你想要的结果是不是:你想限制用户第一位不能输入成"0",但可以输入成12304...之类的,也能输入0.23232之类的,那只能在LostFocus中去处理了。
      

  3.   

    if (i > 0)
    { KeyOut = Keyin; }
    改成
    if (i >= 0)
    { KeyOut = Keyin; }
      

  4.   

    楼上的说的对,最好使用正则表达式,这样也方便维护和修改
    http://www.qostudy.org/pr/Article/jsp/other/200605/26660.html
      

  5.   

    fxqyyzg(海冬青)(昨夜西风凋碧树。独上高楼,望尽天涯路) 
    syringa_12(散步的蠕虫)
    2位朋友,多谢了,你们说的才是我要的
    啥子正则表达式噢,简单才是硬道理!
    请接分