写一个windows应用程序的触发事件代码,怎能当客户在textBox里面输入的字符内容超过多少个时,会在label的Text中提示不能超过!!
还有,运行下面这个事件,当我输入数字时label会提示,可是当我输入其他字符时,就运行不了,为什么,怎么改呢??
 private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (Convert.ToInt64(textBox1.Text )>= 0 || Convert.ToInt64(textBox1.Text) <= 9)
            { label2.Text = "不能输入数字!!"; }
        }帮帮忙

解决方案 »

  1.   

    设置最大字符数属性即可,不需要用事件判断...textBox1.MaxLength = 9;限制输入字符用正则表达式约束...更简单的是用MaskedTextBox控件...
      

  2.   

    但我想在客户超出规定的时候,在下面的label会给点提示
      

  3.   

    if (Convert.ToInt64(textBox1.Text )>= 0 || Convert.ToInt64(textBox1.Text) <= 9) 
    这个方法Convert.ToInt64()在参数为非数字时,转换失败,就出错了
      

  4.   

    Convert转换失败会抛掷异常try
    {
        int value = Convert.ToInt32("Hello");
    }
    catch (Exception)
    {
        // exception occurs here
    }
      

  5.   

     干嘛要吧,textBox1.Text里的内容强制类型转换成int类型呢?
      

  6.   

    就用textBox1.MaxLength进行判断不就行了
      

  7.   

    设置textBox1的MaxLength,当超出时就提示
    因为你输入的值不是数字啊,你用>= 0,<= 9不就是要和数字进行比较的吗  
      

  8.   

    按照你想要的这样,不应该在TextChanged 下面做啊,
    应该在KeyPress中,用下正则
            private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
            {
                if (Regex.IsMatch(e.KeyChar.ToString(), @"\d"))
                {
                    label2.Text = "不能输入数字!!"; 
                }
            }
      

  9.   

    楼上是一个可取方案。但考虑Mouse的上下文菜单时,就复杂了。拙文“定制C# TextBox中只允许输入数字的解决方法”有点思路。
      

  10.   

    那也不要在TextChanged事件中处理,更不要抛异常...有专用于验证输入的事件Validating...例:
    private void textBox1_Validating(object sender, CancelEventArgs e)
    {
    if (System.Text.RegularExpressions.Regex.IsMatch(textBox1.Text, "[0-9]"))
    {
    label2.Text = "不能输入数字";
    e.Cancel = true;
    }
    else
    {
    if (textBox1.Text.Length > 9)
    {
    label2.Text = "不能超过9个字符";
    e.Cancel = true;
    }
    }
    }还有专用于提示用户的控件ErrorProvider...例:
    private void textBox1_Validating(object sender, CancelEventArgs e)
    {
    if (System.Text.RegularExpressions.Regex.IsMatch(textBox1.Text, "[0-9]"))
    {
    errorProvider1.SetError((Control)sender, "不能输入数字");
    e.Cancel = true;
    }
    else
    {
    if (textBox1.Text.Length > 9)
    {
    errorProvider1.SetError((Control)sender, "不能超过9个字符";
    e.Cancel = true;
    }
    }
    }
      

  11.   

    MAXLENGTH 能判断用户输入的长度
    不过对于楼主的想法 也可以判断 KEYPRESS事件  或者 TEXTCHANGE事件。