我想在textbox重判断只允许输入数字和冒号,且第一个不允许为冒号该怎么实现

解决方案 »

  1.   

    private   void   textBox1_TextChanged(object   sender,   EventArgs   e) 
                    { 
                            Regex   r   =   new   Regex(@ "^\d*$|\:"); 
                            if   (!r.IsMatch(this.textBox1.Text)) 
                            { 
                                    textBox1.Text=s;//恢复 
                                    textBox1.SelectionStart   =   s.Length; 
                            } 
                            else 
                                    s=textBox1.Text; 
                            
                    }
      

  2.   

     Regex r = new Regex(@ "^\d*$|\:");  
      if (!r.IsMatch(this.textBox1.Text))  
      {  
      textBox1.Text=s;//恢复  
      textBox1.SelectionStart = s.Length;  
      }  
      else  
      s=textBox1.Text;
      

  3.   

     private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
            {
                if ((e.KeyChar >= '0' && e.KeyChar <= '9') || e.KeyChar == 8)
                {                e.Handled = false;
                }
                else if (e.KeyChar == 58)
                {
                    if (textBox1.Text.Length > 0)
                    {
                        e.Handled = false;
                    }
                    else
                    {
                        e.Handled = true;
                    }
                }
                else
                {
                    e.Handled = true;
                }
            }
      

  4.   

     string s=string.Empty;
                if (textBox1.Text != ":")
                {
                    Regex r = new Regex(@"^\d*$|\:");
                    if (!r.IsMatch(this.textBox1.Text))
                    {
                        textBox1.Text = s;//恢复  
                        textBox1.SelectionStart = s.Length;
                    }
                    else
                        s = textBox1.Text;
                }
                else
                {
                   textBox1.Text=s;
                }
      

  5.   


            private void textBox4_KeyPress(object sender, KeyPressEventArgs e)
            {
                if (e.KeyChar==':')
                {
                    e.Handled = true;
                }
            }数字字母同样实现的方式很多
      

  6.   


    哪不对了,写在KeyPress事件中
      

  7.   

    先事件,keypress事件,在里面用正则判断。。