要在TextBox中只允许输入数字的,如果用户输入字母之类的还要用errorProvider提示错误,怎样做啊?请高手帮忙    用C#  在winform中实现

解决方案 »

  1.   

    string info = ((TextBox)sender).Text;
    for (int i=0 ; i<info.Length ; i++)
    {
    string str = info.Substring(i , 1);
    if ((str.CompareTo("0") >=0 && str.CompareTo("9") <= 0))
    {
    continue;
    }
    else
    {
    MessageBox.Show("输入的值不合法!必须为数字!" , "eFrontSight" 
    , MessageBoxButtons.OK , MessageBoxIcon.Exclamation);
    ((TextBox)sender).Text = "";
    break;
    }
    }
      

  2.   

    private void textRe_TextChanged(object sender, System.EventArgs e)
    {
    string info = ((TextBox)sender).Text;
    for (int i=0 ; i<info.Length ; i++)
    {
    string str = info.Substring(i , 1);
    if ((str.CompareTo("0") >=0 && str.CompareTo("9") <= 0))
    {
    continue;
    }
    else
    {
    MessageBox.Show("输入的值不合法!必须为数字!" , "eFrontSight" 
                             , MessageBoxButtons.OK , MessageBoxIcon.Exclamation);
    ((TextBox)sender).Text = "";
    break;
    }
    }
    }刚才没贴好,重发
      

  3.   

    在TextBox的keyPress中添加如下方法
    if ((e.KeyChar < '0' || e.KeyChar > '9') && e.KeyChar != '\b')
    {
         MessageBox.Show(.................
          e.Handled  = true;
    }
      

  4.   

    建议不要这样做,这样影响用户输入。
    当用户输入时连续报这种提示会使人丧失耐性。
    最好把事件的处理放在KeyPress事件里。 private void txtSessionOutTime_KeyPress(object sender, KeyPressEventArgs e)
            {
                //只能输入数字
                if (e.KeyChar < '0' || e.KeyChar > '9')
                {
                    e.Handled = true;
                }         }
      

  5.   

    最好的解决办法就是如上面的,可以根本上解决
    如果用验证,你用SetError就可以了