【问题标题】:Allowed only comma in KeyDown C#KeyDown C#中只允许逗号
【发布时间】:2017-03-10 00:55:46
【问题描述】:

我屏蔽了键盘上除 1-9 之外的所有键,我遇到了如何启用逗号的问题?

我的代码:

private void textbox_KeyDown(object sender, KeyRoutedEventArgs e)
    if (e.Key >= Windows.System.VirtualKey.Number0 && e.Key <= Windows.System.VirtualKey.Number9 || e.Key >= Windows.System.VirtualKey.NumberPad0 && e.Key <= Windows.System.VirtualKey.NumberPad9 || e.Key == Windows.System.VirtualKey.Decimal)
    {
    e.handled = false;
    }
    else 
    {
    e.handled = true;
    }

【问题讨论】:

    标签: c# uwp


    【解决方案1】:

    此代码将只允许数字和逗号

    if (!char.IsDigit(e.KeyChar) && e.KeyChar != ',')
    {
        e.Handled = true;
    }
    

    另类

     if ((e.KeyChar > (char)Keys.D9 || e.KeyChar < (char)Keys.D0) && e.KeyChar != ',')
    { 
        e.Handled = true; 
    }
    

    【讨论】:

    • 这没有回答问题。提问者使用的是 KeyRoutedEventArgs 而不是 KeyPressEventArgs。
    • 希望link 对@OwenPauling 有所帮助
    • @ArunD 这个答案与问题无关,比有用更令人困惑。没有提及更改 event 类型,因此会引起没有经验的用户的许多问题。
    【解决方案2】:

    试试这个...

    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
            {
                if((e.KeyChar >= 48 && e.KeyChar <= 57) || (e.KeyChar >= 97 && e.KeyChar <= 105))
                {
                    e.Handled = true;
                }
                else
                {
                    e.Handled = false;
                }
            }
    

    【讨论】:

    • 不使用编译器我看到了那个错误:Cannot implicitly convert "KeyPressEventArgs" to "KeyRoutedEventArgs"
    【解决方案3】:

    根据Virtual Key Codes 的文档,您需要 OemComma,即 0xBC 或 (VirtualKey)188。

    【讨论】:

    • 我只能使用 VirualKey.A 或 Del 或 Decimal,我不会写 VirualKey.188
    【解决方案4】:

    你可以试试这个:

            string keyInput = e.KeyChar.ToString();
    
            if (Char.IsDigit(e.KeyChar))
            {
                // Digits are OK
            }
            else if (e.KeyChar == '\b')
            {
                // Backspace key is OK
            }
            else if (e.KeyChar == ',')
            {
                // Comma key is OK
            }
    
    
            else
            {
                // Swallow this invalid key and beep
                e.Handled = true;
                //    MessageBeep();
            }
    

    【讨论】:

      【解决方案5】:

      首先,您必须记住必须在CultureInfo 中设置小数点(.,)。如果您打算向更多受众发布您的应用程序,我建议您牢记这一点。

      另一件事是你的情况毫无意义:

      // this has to be always true
      e.Key >= Windows.System.VirtualKey.Number0 
      && 
      e.Key <= Windows.System.VirtualKey.Number9 
      || // above or below has to be true
      e.Key >= Windows.System.VirtualKey.NumberPad0 
      && // something from above has to be true and below has to be true
      e.Key <= Windows.System.VirtualKey.NumberPad9 
      || // or just decimal mark .. ?
      e.Key == Windows.System.VirtualKey.Decimal
      

      所以继续代码:

      // check for the keys 
      if(
          ( // if numeric between 0 and 9
              e.Key >= Windows.System.VirtualKey.Number0 
              &&
              e.Key <= Windows.System.VirtualKey.Number9
          ) 
          || // or
          ( // numeric from numpad between 0 and 9
              e.Key >= Windows.System.VirtualKey.NumberPad0
              &&
              e.Key <= Windows.System.VirtualKey.NumberPad9 
          )
          || // or decimal mark
          e.Key == Windows.System.VirtualKey.Decimal
      )
      {
          // your logic
      }
      

      请记住,Windows.System.VirtualKey.Decimal 不会返回基于 CultureInfo 的小数点(分隔符),而是来自小键盘的小数点。

      如果你想使用文化信息(国际申请)你可以在CultureInfo.CurrentCulture.NumberFormat.CurrencyDecimalSeparator找到小数点,然后比较文本输入。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多