我是用的VB 我想请问textbox中只能输入数字 怎么写函数 虚心请教

解决方案 »

  1.   

    c#可以么?
    订阅TextBox的KeyPress事件Private void textBox_KeyPress(object sender,KeyPressEventArgs e)
    {
      if((e.KeyChar<48 || e.KeyChar>57) && e.KeyChar!=8)//ASCII 48-57是数字,8是退格
        e.Handled=true;
    }
      

  2.   


    int number;
    bool bl=int.tryparse(strInput,out number);//也可以使用uint.parse
    if(!bl)
    {
    number=1;
    }
    --Regex: ^\d+$ 
      

  3.   

    private bool IsHaveInteger(TextBox tb)
    {
        bool isHave;
        if (tb.Trim().Length == 0)
        {
            isHave = false;
        }
        else
        {
            if (System.Text.RegularExpressions.Regex.IsMatch(tb.Trim().ToString(), @"^[0-9]+$"))
            {
                isHave = true;
            }
            else
            {
                isHave = false;
            }
        }
        return isHave;
    }
      

  4.   

    说一个思路,不过是C#代码 private String perTest = String.Empty; // 记录合理的上一次text
         
            private void mTextBox_TextChanged(object sender, EventArgs e)
            {
                String text = mTextBox.Text; 
                if (String.Empty != text)
                {
                    int curIndex = mTextBox.SelectionStart;
                    int num;
                    if (!int.TryParse(text, out num))
                    {
                        mTextBox.Text = preLabelSize;
                        mTextBox.SelectionStart = curIndex - 1;
                    }
                    else
                    {
                        preLabelSize = mTextBox.Text;
                    }
                }
            }这样就实现你说的效果,并且光标停在上次合理的位置。
      

  5.   

    perTest = mTextBox.Text; 
    Sorry!
      

  6.   

    看你是验证整数还是小数
       public bool CheckDouble(char a)
            {
                if((a<48 || a>57)&&(a!=46)&&(a!=8))
                {
                    return false;
                }
                return true;
            }
            public bool CheckInt(char a)
            {
                if ((a < 48 || a > 57) &&(a != 8))
                {
                    return false;
                }
                return true;
            }然后再写textbox的KeyPress事件
       private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
            {
                if (!CheckInt(e.KeyChar))
                {
                    e.Handled = true;
                }
            }
      

  7.   

    try
    {
    int.parse(textbox.text);
    }
    catch(Exception ex)
    {
    MessageBox.Show(ex.Message);
    }