小弟初学C# 想问下如何才能让文本框无法输入空格
在网上找到两个判断
            if (e.KeyChar.ToString() == " ")
                e.Handled = true;
            if (e.KeyChar == 32)
                e.Handled = true;虽然目的达到了,但是空格依然可以复制进去
别的地方说用正则表达式判断 可惜小弟不会呃 5555顺便求个文本框只能输入数字的判断 谢谢啊~~

解决方案 »

  1.   

    顺便问下 那个e.KeyChar == 32是什么意思?
    为什么等于了32就不能输入空格了呢?
    第一个到还能看明白
    第二个就不明白了
      

  2.   

    那就在事件里再替换一下吧private void textBox1_TextChanged(object sender, EventArgs e)
    {
        textBox1.Text = textBox1.Text.Replace(" ", "");
    }
      

  3.   


    判断输入进来的ASC码 
      

  4.   


    if (e.KeyChar >= 48 && e.KeyChar <= 57)
    {
        e.Handled = true;
    }
      

  5.   

    32 空格ASCII码
    private void TextBox_KeyPress(object sender, KeyPressEventArgs e)
    {
        if(e.KeyChar!='')
        {
            e.Handled = true;
        }
    }
      

  6.   


    List<char> list = new List<char>(new char[]{'+', '-', '*', '/', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'});
    if (!list.Contains(e.KeyChar))
        e.Handled = true;
    //或者
    if (!Regex.IsMatch(e.KeyChar.ToString(), "[-+*/0-9]"))
        e.Handled = true;
      

  7.   


                if (!(e.KeyChar >= '0' && e.KeyChar <= '9') && e.KeyChar != (char)8)
                {
                    e.Handled = true;
                }哪位大大 能给小弟翻译下上面那代码啥意思?
    先谢谢了~~
      

  8.   


    是写在文本框的KeyPress事件里的 功能是只能输入数字 别的不让输入但是看了半天 没看明白是怎么回事!求明白人给小弟解释下