【问题标题】:Silverlight textbox to accept only decimalsSilverlight 文本框只接受小数
【发布时间】:2012-01-28 11:04:54
【问题描述】:

我有一个带有文本框的 Silverlight 应用程序,我只想将其输入限制为十进制数字。在网上搜索我遇到了以下可能的解决方案(奇怪的是在不同的地方,不同的人声称同一行代码的作者身份) 它似乎工作得很好,除了在输入至少 1 个数字后,它将允许输入大写或小写的字母“d”,我不知道为什么会这样,因此不知道如何防止这种情况发生。谁能提供一个解决方案。非常感谢。

    private void Unit_KeyDown(object sender, KeyEventArgs e)
    {

        if (e.Key == Key.Tab)
        {

        }
        var thisKeyStr = "";
        if (e.PlatformKeyCode == 190 || e.PlatformKeyCode == 110)
        {
            thisKeyStr = ".";
        }
        else
        {
            thisKeyStr = e.Key.ToString().Replace("D", "").Replace("NumPad", "");
        }
        var s = (sender as TextBox).Text + thisKeyStr;
        var rStr = "^[0-9]+[.]?[0-9]*$";
        var r = new Regex(rStr, RegexOptions.IgnoreCase);
        e.Handled = !r.IsMatch(s);

    }

【问题讨论】:

    标签: c# regex silverlight


    【解决方案1】:

    您可以尝试以下方法:

    1. else 替换为else if (e.Key != Key.D)
    2. 像这样设置Handled 属性:

      e.Handled = !r.IsMatch(s) || string.IsNullOrEmpty(thisKeyStr);
      
      // also possible:
      e.Handled = !r.IsMatch(s) || e.Key == Key.D;
      

    【讨论】:

    • 谢谢,您的 //also possible: 是似乎最适合我的解决方案,再次感谢。
    【解决方案2】:

    这是一个更容易优化的代码。没有对象创建;没有字符串比较和正则表达式验证

    private static void TextBox_KeyDown(object sender, KeyEventArgs e)
        {
            //platform code for Hyphen which is not same as Subtract symbol but in our case both give same meaning
            const int KEYCODE_Hyphen_OnKeyboard = 189;
            const int KEYCODE_Dot_OnKeyboard = 190;
            const int KEYCODE_Dot_OnNumericKeyPad = 110;
    
            e.Handled = !(
                (!( //No modifier key must be pressed
                     (Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift
                  || (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control
                  || (Keyboard.Modifiers & ModifierKeys.Alt) == ModifierKeys.Alt
                )
              && ( //only these keys are supported
                    (e.Key >= Key.D0 && e.Key <= Key.D9) || (e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9)
                  || e.Key == Key.Subtract || e.Key == Key.Add || e.Key == Key.Decimal
                  || e.Key == Key.Home || e.Key == Key.End || e.Key == Key.Delete
                  || e.Key == Key.Tab || e.Key == Key.Enter || e.Key == Key.Escape || e.Key == Key.Back
                  || (e.Key == Key.Unknown && (
                             e.PlatformKeyCode == KEYCODE_Hyphen_OnKeyboard
                          || e.PlatformKeyCode == KEYCODE_Dot_OnKeyboard || e.PlatformKeyCode == KEYCODE_Dot_OnNumericKeyPad
                        )
                     )
                 )
              )
            );
        }
    

    【讨论】:

    • 这是一个很好的解决方案,但是,禁止修饰符会禁止用户使用键盘进行选择、复制、剪切或粘贴。
    【解决方案3】:
          private void TextBox_KeyDown(object sender, KeyEventArgs e)
        {
            bool isDigit = e.Key >= Key.D0 && e.Key < Key.D9 || e.Key == Key.NumPad0 || e.Key == Key.NumPad1 || e.Key == Key.NumPad2 || e.Key == Key.NumPad3 || e.Key == Key.NumPad4 || e.Key == Key.NumPad5 || e.Key == Key.NumPad6 ||
            e.Key == Key.NumPad7 || e.Key == Key.NumPad8 || e.Key == Key.NumPad9 ||e.Key == Key.Back || e.Key == Key.Delete || e.Key == Key.Left || e.Key == Key.Right;
    
            if (isDigit) { }
            else
                e.Handled = true; 
        }
    

    【讨论】:

      猜你喜欢
      • 2022-06-28
      • 2011-06-15
      • 1970-01-01
      • 1970-01-01
      • 2018-12-28
      • 2013-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多