if (!char.IsDigit(e.KeyChar) && e.KeyChar != 8)
                e.Handled = true;以上是只能输入数字
请问:怎么让文本框也能输入小数点。

解决方案 »

  1.   

    private void textBox2_KeyPress(object sender, KeyPressEventArgs e) 
            { 
                //检测是否已经输入了小数点 
                bool IsContainsDot = this.textBox2 .Text.Contains("."); 
                if ((e.KeyChar < 48 || e.KeyChar > 57) && (e.KeyChar != 8) && (e.KeyChar != 46)) 
                { 
                    e.Handled = true; 
                } 
                else if (IsContainsDot && (e.KeyChar == 46)) //如果输入了小数点,并且再次输入 
                { 
                    e.Handled = true; 
                } 
        
            }