在控件的KeyPress事件中,如果当前系统为中文输入法判断e.KeyChar好象无效,比如我只想在某个文本框中输入数字该怎么做(字母和汉字不允许输入)?

解决方案 »

  1.   

    http://www.h2o.name/post/Code_Collect.html
      

  2.   

    试试 控件的TextChanged事件
      

  3.   

    if (e.KeyChar < Convert.ToInt32('0') || e.KeyChar >  Convert.ToInt32('9'))
    e.Handled = true;
      

  4.   

    用rangevalidator或者直接try{int value = Int32.Parse()}
      

  5.   

    private static bool CheckIsNum(System.Windows.Forms.Control p_txtBox,System.Windows.Forms.KeyPressEventArgs e)
            {
    if ((e.KeyChar  >= (char)48 && e.KeyChar<=(char)57) || 
     e.KeyChar ==(char)8 || e.KeyChar ==(char)45 || e.KeyChar ==(char)46)
    {
    }
    else
    {
                    MessageBox.Show("輸入的字串有誤﹐請重新輸入﹗","系統信息",MessageBoxButtons.OK,MessageBoxIcon.Error);
    e.Handled=true;  
    }
                return true;
            }