2012-04-20 85 views
6

我在C#WinRT应用程序中构建表单,并且想要将其中一个TextBox组件中的字符限制为数字。 (此文本框将供用户输入一年。)限制文本框中的字符

我已经搜索了一段时间,但一直未能在TextChanged事件中未设置事件侦听器的情况下计算出此事件,检查每个按键上的text属性。有没有办法简单地说用户只能在TextBox中输入特定的字符?

回答

8

能够工作是根据您的规则绑定到OnTextChanged事件和修改文本的最简单的事情。

<TextBox x:Name="TheText" TextChanged="OnTextChanged" MaxLength="4"/> 
private void OnTextChanged(object sender, TextChangedEventArgs e) 
    { 
     if (TheText.Text.Length == 0) return; 

     var text = TheText.Text; 

     int result; 
     var isValid = int.TryParse(text, out result); 
     if (isValid) return; 

     TheText.Text = text.Remove(text.Length - 1); 
     TheText.SelectionStart = text.Length; 
    } 

不过,我会回避这种方式离开,因为地铁的口头禅是触摸,第一UI,你可以很容易做到这一点与FlipView控制触摸优先的方式。

+0

标记为答案,因为它大多解决了我遇到的问题。这将限制文本框只显示数字。但是,自从我重新访问了我的应用程序的这一部分,并用一个ComboBox替换了年份字段,完全避开了这个问题。 (我已经有了一个固定的年限范围,所以我不知道为什么我没有首先使用组合框。) – 2012-04-26 04:19:42

-2

使用MaskedTextBox控件。仅限数字,只需使用Mask属性指定字符和长度(如果有)。例如如果您只想输入五个数字,则将mask属性设置为“00000”。就那么简单。 Windows为您处理限制。

+1

是否有一个用于WinRT的MaskedTextBox? – 2012-04-20 01:50:57

+0

@RitchMelton我目前不知道。我想答案应该是肯定的。 – olatunjee 2012-04-20 01:56:32

+0

当前文档说不。 http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.aspx – 2012-04-20 02:02:02

0

有效年份

DateTime newDate; 
var validYear = DateTime.TryParseExact("2012", "yyyy", CultureInfo.InvariantCulture, 
DateTimeStyles.None, out newDate); //valid 

无效的年份

var validYear = DateTime.TryParseExact("0000", "yyyy", CultureInfo.InvariantCulture, 
DateTimeStyles.None, out newDate); //invalid 
+0

http://stackoverflow.com/editing-help#code – 2012-04-20 02:43:04

6

尝试TextBox.InputScope属性设置为InputScopeNameValue.Number,在MSDN中Guidelines and checklist for text input提及。

+0

这很酷,并且(种类)也存在于WPF中。不知道这个。谢谢! – 2012-04-20 08:10:53

+0

+1整洁的功能 – 2012-04-20 09:53:22

+1

这种有点作品,但不会做我正在努力完成的。它看起来像InputScope属性更像是系统提示系统应该显示哪个软键盘,但它不会创建一个屏蔽的文本框来限制输入。 (最后在MSDN论坛中发现了这个主题 - 下面的链接,请参阅Chipalo Street发布的帖子,从2012年3月22日星期四下午5:49)。 http://social.msdn.microsoft。com /论坛/ en-US/winappswithcsharp /线程/ d746d7d7-91c7-4429-8efa-a748b4ce6a03 – 2012-04-23 14:14:39

0

这似乎为我工作:

private void TextBox_KeyDown(object sender, KeyRoutedEventArgs e) 
    { 
     if ((e.Key < VirtualKey.Number0) || (e.Key > VirtualKey.Number9)) 
     { 
      // If it's not a numeric character, prevent the TextBox from handling the keystroke 
      e.Handled = true; 
     } 
    } 

所有值请参阅VirtualKey enumeration的文档。

0

根据在link的发帖,添加选项卡以允许导航。

private void decimalTextBox_KeyDown(object sender, KeyRoutedEventArgs e) 
    { 
     bool isGoodData; // flag to make the flow clearer. 
     TextBox theTextBox = (TextBox)sender; // the sender is a textbox 

     if (e.Key>= Windows.System.VirtualKey.Number0 && e.Key <= Windows.System.VirtualKey.Number9) // allow digits 
      isGoodData = true; 
     else if (e.Key == Windows.System.VirtualKey.Tab) 
      isGoodData = true; 
     else if (e.Key >= Windows.System.VirtualKey.NumberPad0 && e.Key <= Windows.System.VirtualKey.NumberPad9) // allow digits 
      isGoodData = true; 
     else if (e.Key == Windows.System.VirtualKey.Decimal || (int)e.Key == 190) // character is a decimal point, 190 is the keyboard period code 
                        // which is not in the VirtualKey enumeration 
     { 
      if (theTextBox.Text.Contains(".")) // search for a current point 
       isGoodData = false; // only 1 decimal point allowed 
      else 
       isGoodData = true;  // this is the only one. 
     } 
     else if (e.Key == Windows.System.VirtualKey.Back) // allow backspace 
      isGoodData = true; 
     else 
      isGoodData = false; // everything else is bad 
     if (!isGoodData)   // mark bad data as handled 
      e.Handled = true; 
    } 
+1

Shift-4,应该是美元符号,不会用此代码过滤掉。 4键被看作并被接受为数字。看到这样的问题: http://stackoverflow.com/questions/13001215/detect-if-modifier-key-is-pressed-in-keyroutedeventargs-event其中示出用于检测的移位,控制各种方法 等钥匙。 – 2015-01-19 00:57:21