【问题标题】:How to stop the first character in a text box from being '.'?如何阻止文本框中的第一个字符为“。”?
【发布时间】:2012-05-02 10:40:54
【问题描述】:

这是我目前拥有的代码:

private void textBox_KeyPress(object sender, KeyPressEventArgs e)
{
    e.Handled = !char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar) && e.KeyChar != '.';
    if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1) e.Handled = true; 

}

【问题讨论】:

  • 这段代码有什么问题?
  • 我想输入一个双变量..例如“1.1”..我想阻止它输入“.1”
  • 所以,你是说你现在拥有的代码阻止你在文本框中输入. anywhere,但你only想要防止 first 字符成为 .?
  • 我现在拥有的代码让我可以在表单的任何位置输入它并阻止它输入多个。但我想阻止它作为文本框中的第一个字符输入
  • 确保考虑到有人将数字粘贴到文本框中的情况。请不要通过禁用粘贴来解决它。

标签: c# .net winforms textbox keypress


【解决方案1】:

你可以试试这个:

private void TextBox_TextChanged(object sender, EventArgs e)        
{        
        TextBox.Text = TextBox.Text.TrimStart('.');        
}

【讨论】:

    【解决方案2】:

    这也适用于复制和粘贴。

        private void textBox1_KeyUp(object sender, KeyEventArgs e)
        {
            int decimalCount=0;
            string rebuildText="";
            for(int i=0; i<textBox1.Text.Length; i++)
            {
                if (textBox1.Text[i] == '.')
                {
                    if (i == 0) break;
                    if (decimalCount == 0)
                        rebuildText += textBox1.Text[i];
                    decimalCount++;
                }
                else if ("0123456789".Contains(textBox1.Text[i]))
                    rebuildText += textBox1.Text[i];
            }
            textBox1.Text = rebuildText;    
            textBox1.SelectionStart = textBox1.Text.Length;
    
        }
    

    【讨论】:

    • 这不限于一个'.'在整个事情。您的 cmets 中的哪一个似乎是您想要的。但它回答了主要问题。
    【解决方案3】:

    您可能想要使用 TextChanged 事件,因为用户可以粘贴值。为了获得满足要求的最佳体验,我建议简单地删除任何前导 . 字符。

    void textBox1_TextChanged(object sender, EventArgs e)
    {
      if (textBox1.Text.StartsWith("."))
      {
        textBox1.Text = new string(textBox1.Text.SkipWhile(c => c == '.').ToArray());
      }
    }
    

    这并没有解决仅使用数字的要求——如果是这种情况,问题中并不清楚。

    【讨论】:

      【解决方案4】:

      KeyPress 不足以进行这种验证。绕过它的一种简单方法是使用 Ctrl+V 将文本粘贴到文本框中。或者上下文菜单,根本没有按键事件。

      在这种特定情况下,TextChanged 事件将完成工作:

          private void textBox_TextChanged(object sender, EventArgs e) {
              var box = (TextBox)sender;
              if (box.Text.StartsWith(".")) box.Text = "";
          }
      

      但验证数值还有很多其他内容。您还需要拒绝 1.1.1 或 1.-2 等内容。请改用 Validating 事件。在表单上放置一个 ErrorProvider 并像这样实现事件:

          private void textBox_Validating(object sender, CancelEventArgs e) {
              var box = (TextBox)sender;
              decimal value;
              if (decimal.TryParse(box.Text, out value)) errorProvider1.SetError(box, "");
              else {
                  e.Cancel = true;
                  box.SelectAll();
                  errorProvider1.SetError(box, "Invalid number");
              }
          }
      

      【讨论】:

      • 感谢您的回答,它有效,也感谢有关 CTRL+V 的建议
      猜你喜欢
      • 2014-04-16
      • 2015-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-10
      相关资源
      最近更新 更多