文本框验证允许一个“。”值c#

时间:2012-12-08 05:45:00

标签: c# .net winforms textbox

我希望文本框验证只允许一个.值和数字。意味着我的文本框值应仅包含数值和一个.值。价值应该是123.50。 我正在使用代码在我的值末尾添加.oo.50值。 我的代码是

double x;
double.TryParse(tb.Text, out x);
tb.Text = x.ToString(".00");

它从键盘上取下所有键,但我想只取一个数字和一个.值。

8 个答案:

答案 0 :(得分:7)

为您的文本框添加Control.KeyPress事件处理程序。

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (!char.IsControl(e.KeyChar))  //bypass control keys
    {
        int dotIndex = textBox1.Text.IndexOf('.');
        if (char.IsDigit(e.KeyChar))     //ensure it's a digit
        {   //we cannot accept another digit if
            if (dotIndex != -1 &&  //there is already a dot and
                //dot is to the left from the cursor position and
                dotIndex < textBox1.SelectionStart &&
                //there're already 2 symbols to the right from the dot
                textBox1.Text.Substring(dotIndex + 1).Length >= 2)
            {
                e.Handled = true;
            }
        }
        else //we cannot accept this char if
            e.Handled = e.KeyChar != '.' || //it's not a dot or
                        //there is already a dot in the text or
                        dotIndex != -1 ||   
                        //text is empty or
                        textBox1.Text.Length == 0 || 
                        //there are more than 2 symbols from cursor position
                        //to the end of the text
                        textBox1.SelectionStart + 2 < textBox1.Text.Length;
    }
}

你可以通过设计师或你的构造函数这样做:

public Form1()
{
    InitializeComponent();
    //..other initialization
    textBox1.KeyPress += new KeyPressEventHandler(textBox1_KeyPress);
}

我还添加了几项检查,以确保您不仅可以在文本的末尾插入数字,还可以在任何位置插入数字。与点相同。它控制你点的右边不超过2位数。我已经使用TextBox.SelectionStart Property来获取文本框中光标的位置。请查看此主题以获取有关该主题的更多信息:How do I find the position of a cursor in a text box?

答案 1 :(得分:1)

尝试这个

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (!char.IsControl(e.KeyChar)
               && !char.IsDigit(e.KeyChar)
               && e.KeyChar != '.')
            e.Handled = true;

        // only allow one decimal point
        if (e.KeyChar == '.'
            && textBox1.Text.IndexOf('.') > -1)
            e.Handled = true;
    }

答案 2 :(得分:1)

在textBox的keyPress事件中,你可以这么做......

        e.Handled = !char.IsDigit(e.KeyChar)&&(e.KeyChar != '.') && !char.IsControl(e.KeyChar);
        if ((e.KeyChar == '.') && ((sender as TextBox).Text.IndexOf('.') > -1))
        {
            e.Handled = true;
        }

答案 3 :(得分:0)

尝试此代码,只需替换您想要的输入类型'validinpu'字符串。

try
{
    short charCode = (short)Strings.Asc(e.KeyChar);
    string validinput = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-0123456789 .";
    if (Strings.InStr(validamt, Conversions.ToString(Strings.Chr(charCode)), Microsoft.VisualBasic.CompareMethod.Binary) == 0)
    {
        charCode = 0;
    }
    if (charCode == 0)
    {
        e.Handled = true;
    }
}

答案 4 :(得分:0)

另一个例子,

 private void txtPrice_KeyPress(object sender, KeyPressEventArgs e)
    {
         // To disallow typing in the beginning writing
        if (txtPrice.Text.Length == 0)
        {
            if (e.KeyChar == '.')
            {
                e.Handled = true;
            }
        }
        if (!char.IsDigit(e.KeyChar) && e.KeyChar != 8 && e.KeyChar != 46)
        {
            e.Handled = true;
        }
        if (e.KeyChar == '.' && txtPrice.Text.IndexOf('.') > -1)
        {
            e.Handled = true;
        }
    }

答案 5 :(得分:0)

也尝试这个简短的

e.Handled = (!(e.KeyChar == (char)Keys.Back || e.KeyChar == '.')); //allow  dot and Backspace
e.Handled = (e.KeyChar == '.' && TextBox1.Text.Contains(".")); //allow only one dot

此示例仅允许一个退格

答案 6 :(得分:0)

 if (textBox.Text!="")
        {
            string txt = textBox.Text;
            if (e.KeyChar.ToString().Any(Char.IsNumber) || e.KeyChar == '.')
            {
                textBox.Text = rate;
            }
            else
            {
                MessageBox.Show("Number Only", "Warning");
                textBox.Text = "";
            }
        }

我测试过的代码

答案 7 :(得分:-1)

if(e.KeyChar.Equals('\b'))
 {
  e.Handled = false;
 }
else
 if (!char.IsControl(e.KeyChar)
           && !char.IsDigit(e.KeyChar)
           && e.KeyChar != '.')
  {
        e.Handled = true;
  }
 else
    // only allow one decimal point
    if (e.KeyChar == '.'
        && textBox1.Text.IndexOf('.') > -1)
  {        
    e.Handled = true;  
  }