【问题标题】:Input only digits and control buttons仅输入数字和控制按钮
【发布时间】:2019-09-11 11:03:24
【问题描述】:

我想输入任何值的Salary:550,49、2222,12、9,3 等等。但是需要像这样使用控制按钮:,,backspace,ctrl + c,ctrl + v,ctrl + a。

Salary 是 TextBox 与 ShortcutsEnabled = true 和事件:

private void TbSalary_KeyPress(object sender, KeyPressEventArgs e)
{
    char number = e.KeyChar;
    if ((e.KeyChar <= 47 || e.KeyChar >= 58) && number != 8 && number != 44) 
         //digits, BackSpace and ,
    {
        e.Handled = true;
    }
}

如果删除此条件,指定的组合将起作用。但不仅输入数字。

我应该在此处添加对所有组合的跟踪吗?或者是否有可能以其他方式实现此任务?

MaskedTextBox 需要固定数量的字符和一些“掩码”。但Salary 不同。可以是**,**、******,*或***等。

更新

防止在小数点后输入两个以上的数字

if (number < ' ')
{
    return;
}
if (number >= '0' && number <= '9')
{
    if (this.Text.Contains(',') 
        && this.SelectionLength == 0
        && this.SelectionStart > this.Text.IndexOf(',')
        && this.Text.Length - this.Text.IndexOf(',') > 2)
    {
        e.Handled = true;
    }

    return;
}

【问题讨论】:

    标签: c# winforms textbox maskedtextbox


    【解决方案1】:

    请不要使用像47 这样的幻数,让我们使用字符。我们应该允许这些字符:

    • '0'..'9' 范围(数字)
    • tab、backspace 等的控制字符(在空格 ' ' 下方)
    • ','(逗号)作为小数分隔符

    所有其他角色都应该被禁止。

    代码:

    private void TbSalary_KeyPress(object sender, KeyPressEventArgs e)
    {
        char number = e.KeyChar;
        TextBox box = sender as TextBox;
    
        if (number >= '0' && number <= '9' || number < ' ')
            return; // numbers as well as backspaces, tabs: business as usual
        else if (number == ',') {  
            // We don't want to allow several commas, right?
            int p = box.Text.IndexOf(',');
    
            // So if we have a comma already...
            if (p >= 0) { 
                // ... we don't add another one
                e.Handled = true;
    
                // but place caret after the comma position
                box.SelectionStart = p + 1;
                box.SelectionLength = 0;  
            }  
            else if (box.SelectionStart == 0) {
                // if we don't have comma and we try to add comma at the 1st position 
                e.Handled = true;
    
                // let's add it as "0,"
                box.Text = "0," + box.Text.Substring(box.SelectionLength);
                box.SelectionStart = 2;
           } 
        }
        else  
            e.Handled = true; // all the other characters (like '+', 'p') are banned
    }
    

    请注意,有可能粘贴 不正确的值(比如"bla-bla-bla")到TbSalary TextBox;为防止它,您可以使用TextChanged 事件:

    private void TbSalary_TextChanged(object sender, EventArgs e) {
      TextBox box = sender as TextBox;
    
      StringBuilder sb = new StringBuilder();
    
      bool hasComma = false;
    
      foreach (var c in box.Text)
        if (c >= '0' && c <= '9')
          sb.Append(c);
        else if (c == ',' && !hasComma) {
          hasComma = true;
    
          if (sb.Length <= 0) // we don't start from comma
            sb.Append('0');
    
          sb.Append(c);
        }
    
      string text = sb.ToString();
    
      if (!text.Equals(box.Text))
        box.Text = text;
    }
    

    【讨论】:

    • 您能告诉我为什么在我的情况下Ctrl 组合被阻止了吗?对于完全控制缺少大小写,当逗号强加第一个时。错误或暗示0,**
    • @Viewed:如果您想允许所有控制键(如backspace、ctrl+v 等),您应该允许控制字符(空格下方): number &lt; ' '
    • @Viewed:当我们处于第一个位置并按逗号并希望使用"0,..." 而不是",..." 时,您可以轻松实现一个案例(请参阅我的编辑)
    • 看看我尝试禁止在小数点后输入两个以上的数字(更新后)。
    • @Viewed:对不起,我不买。如果我有"123456,78" 插入符号在2 - "12|3456,78" 之后并且我按0 怎么办?如果您开始调整您应该使用box.SelectionStart 和box.SelectionLength 操作的解决方案。在您的情况下:如果逗号存在且不在选择范围内,如果插入符号位置在逗号之后,则...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-22
    • 2018-02-08
    • 2013-10-21
    • 2011-05-18
    • 2011-05-28
    相关资源
    最近更新 更多