private void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ("0123456789".IndexOf(e.KeyChar) == -1) e.Handled = true;
        }    如上 实现comboBox 只能数字输入。  但是这个代码却导致不能按删除键 ←(Keys.Back) . 只能用Delete 键来删除输入的字体。
     请问如何实现这个代码能用删除键删除输入的字   或者 。。  不要这个代码也可以 。。  但我要求,只能输入数字 。 也要能删除键可用 !  各位大虾赐教  。。

解决方案 »

  1.   


     private void combobox_KeyPress(object sender, KeyPressEventArgs e)
            {            if (!char.IsDigit(e.KeyChar) && e.KeyChar != (char)8) //  放弃该输入
                {
                    e.Handled = true;
                    return;
              }
      

  2.   


             private bool nonNumberInput = false;         private void comboBox1_KeyDown(object sender, KeyEventArgs e)
             {
                 nonNumberInput = false;
                 if ((e.KeyCode < Keys.D0) || ((e.KeyCode > Keys.D9) && (e.KeyCode < Keys.NumPad0)) || (e.KeyCode > Keys.NumPad9))
                 {
                     if (e.KeyCode != Keys.Back)
                     {
                         nonNumberInput = true;
                     }
                 }
             }         private void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
             {
                 string TempStr = "!@#$%^&*()";
                 if (TempStr.Contains(e.KeyChar) || nonNumberInput)
                 {
                     e.Handled = true;
                 }
             }
      

  3.   

    也可以这样:         private bool IsPressBackSpace = false;         private void comboBox1_KeyDown(object sender, KeyEventArgs e)
             {
                 IsPressBackSpace = false;
                 if (e.KeyCode == Keys.Back) { IsPressBackSpace = true; }
             }         private void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
             {
                 if (IsPressBackSpace) { return; }             //string TempString = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
                 string TempNumber = "0123456789";
                 e.Handled = !TempNumber.Contains(e.KeyChar);
             }还可以使用正则,但我不会
      

  4.   

    private   void   comboBox1_KeyPress(object   sender,   System.Windows.Forms.KeyPressEventArgs   e)   
      {   
      if(e.KeyChar   >=   '0'   &&   e.KeyChar   <=   '9' && e.KeyChar !=  (char)Keys.Back)   
      {   
      e.Handled   =   false;   
      }   
      else   
      {   
      e.Handled   =     true;   
      }   
      }   
        
      

  5.   

    private void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if ("0123456789".IndexOf(e.KeyChar) == -1 && e.KeyChar != 8) e.Handled = true;
    }  
      

  6.   

    private void comboBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)   
      {   
      if(e.KeyChar >= '0' && e.KeyChar <= '9' && e.KeyChar != (char)Keys.Back)   
      {   
      e.Handled = false;   
      }   
      else   
      {   
      e.Handled = true;   
      }   
      }   
      

  7.   

    先判断按键是否是删除除,如果是则return
      

  8.   


    using System.Windows.Forms;namespace WindowsFormsApplication19
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
            {
            }        private void comboBox1_KeyDown(object sender, KeyEventArgs e)
            {
                if (!((e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9) || (e.KeyCode == Keys.Delete)
                    || (e.KeyCode == Keys.Back) || (e.KeyCode == Keys.Left) 
                    || (e.KeyCode == Keys.Right)))
                {
                    /// 拦截该键
                    e.SuppressKeyPress = true;
                }
            }
        }
    }