【问题标题】:How to make the Textbox numeric in wpf application如何在 wpf 应用程序中使文本框数字化
【发布时间】:2019-09-08 00:26:21
【问题描述】:

我想将文本框设为数字,我尝试了这个功能:

   <TextBox Name="txtProductId" PreviewTextInput="Number_Validation"/>


   public static void Number_Validation(object sender,System.Windows.Input.TextCompositionEventArgs e)
   {
       System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex("[^0-9]+");
       e.Handled = regex.IsMatch(e.Text);
   }

但它接受“空格”和数字。我不需要空间。

【问题讨论】:

  • ^[0-9]*$,试试看.. 或[\d]
  • 当我使用 ^[0-9]*$ 它接受任何字母和数字时,[\d] 对我不起作用
  • 您可以从其他 SO 答案中剪切和粘贴样本。例如stackoverflow.com/questions/1268552/…
  • 看看this post,它让您有机会仅在文本框中接受数值。

标签: c# wpf textbox numeric


【解决方案1】:

在正则表达式中,您需要定义在输入的开始 (^) 和结束 ($) 之间,只有十进制字符有效,在这种情况下,表达式看起来像

^\d+$

要检查您的具体要求,请使用Regex101 Online

【讨论】:

    【解决方案2】:

    "^[0-9]+" 。 ^ 应该在外面

    【讨论】:

    • 当 ^ 在外面时,它接受任何字母和数字
    猜你喜欢
    • 2011-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多