C#中如何限制comboBox中只能输入字母,其他的字符不能输入。

解决方案 »

  1.   


    private void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        e.Handled = e.KeyChar < '0' || e.KeyChar > '9'; 
        if (e.KeyChar == (char)8) 
            e.Handled = false; 
    }
      

  2.   

    private void comboBox1_Validating(object sender, CancelEventArgs e)
            {
                string pattern = @"^[A-Za-z]*$";
                System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(pattern);
                if (!reg.IsMatch(this.comboBox1.Text))
                {
                    e.Cancel = true;
                }
            }输入不正确焦点则不能离开comboBox控件,或者在comboBox1_TextChanged事件中进行判断也可
      

  3.   

    if (e.KeyChar>='a'&&e.KeyChar<='z')
    {
    e.Handled = false;
    }
    else
    {
    if((e.KeyChar !=8)&&(e.KeyChar !=13))
    {
    e.Handled = true;
    }
    }
    自己刚才整了个似乎好用
      

  4.   

    using System.Text.RegularExpressions;string regular = @"^[A-Za-z]*$";
    Match m = Regex.Match(comboBox1.Text, regular);
    if (m.Success)
    {
      ...
    }
    else
    {
      ...
    }
      

  5.   

    LZ考不考虑输入空格这些的问题?如果可以输入,判断KEYCHAR的时候还要加BACK
      

  6.   

    在comboBox1_KeyPress事件中写代码控制,代码他们已经写了。