C# 仅输入数字和控制按钮

C# 仅输入数字和控制按钮,c#,winforms,textbox,maskedtextbox,C#,Winforms,Textbox,Maskedtextbox,我想用任意值输入工资:550,49,2222,12,9,3等等。但是需要像这样使用控制按钮:,,退格,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.KeyCh

我想用任意值输入工资:
550,49
2222,12
9,3
等等。但是需要像这样使用控制按钮:
退格
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;
    }
}
请不要使用像
47
这样的神奇数字,让我们使用字符。我们应该允许这些字符:

  • '0'..'9'
    范围(数字)
  • 用于
    选项卡
    退格
    等的控制字符(位于空格
    '
    下方)
  • “,”(逗号)作为十进制分隔符
所有其他角色都应该被禁止

代码:

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
}

你能告诉我为什么在我的情况下,
Ctrl
组合被阻止了吗?对于完全控制,当逗号强制第一个时,缺少大小写。错误或暗示
0,***
@查看:如果您想允许所有控制键(如
backspace
ctrl+v
等),您应该允许控制字符(位于空格以下):
number<'
@查看:当我们位于第一个位置并按逗号并想要查看
“0,…”
而不是
,…”
(参见我的编辑)看看我试图禁止在小数点后输入两个以上的数字(更新帖子)。@查看:对不起,我不买它。如果我有
“123456,78”
插入符号在
2
-
“12 | 3456,78”之后怎么办“
然后我按
0
?如果开始优化解决方案,则应使用
box.SelectionStart
box.SelectionLength
操作。在您的情况下:如果逗号存在且不在选择范围内,如果插入符号位置在逗号之后,则。。。
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
}
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;
}