2017-08-17 23 views
1

我有一个WPF应用程序(MVVM)。我想限制用户在TextBox中输入多个特定值。 假设该值为'100',那么用户应该不能输入101等。我试过下面的代码。限制TextBox输入达到特定值而不会干扰光标位置

XAML:

<TextBox Text="{Binding SearchText}" FocusManager.FocusedElement="{Binding RelativeSource={RelativeSource Self}}" Name="SearchTextBox" TextChanged="TextBox_TextChanged"/> 

CODE:

private void TextBox_TextChanged(object sender, TextChangedEventArgs e) 
    { 
     TextBox textBox = sender as TextBox; 
     if (textBox != null && !string.IsNullOrEmpty(textBox.Text)) 
     { 
      int searchIndex = 0; 
      int count = 100; 
      int.TryParse(textBox.Text, out searchIndex); 

      if (textBox != null && !string.IsNullOrEmpty(textBox.Text)) 
      { 
       if (searchIndex > Count) 
       { 
        textBox.Text = textBox.Text.Substring(0, textBox.Text.Length - 1); 
       } 
      } 
     } 
    } 

有了这个代码,我能够进入比特定值更限制用户。但问题是,当我设置文本框的文本,然后光标移动到第一个数字。有没有解决方案?

+0

更好的找到wpf的NumericUpDown控件 – ASh

回答

1

One & two。在这两个答案的帮助下,我解决了我的问题。我正在处理两个事件PreviewKeyDown & TextChanged。

代码:

private void TextBox_TextChanged(object sender, TextChangedEventArgs e) 
    { 
      TextBox textBox = sender as TextBox; 
      int searchIndex = 0; 
      int Count = 100; 
      int.TryParse(textBox.Text, out searchIndex); 
      if (textBox != null && !string.IsNullOrEmpty(textBox.Text)) 
      { 
       if (searchIndex > Count) 
       { 
        textBox.Text = OldValue.ToString(); 
        textBox.SelectionStart = start; 
       } 
      } 
     } 

public int OldValue = 0; 
public int start = 0; 


private void SearchTextBox_PreviewKeyDown(object sender, KeyEventArgs e) 
    { 
     TextBox textBox = sender as TextBox; 
     int.TryParse(textBox.Text, out OldValue); 
     start = textBox.SelectionStart; 
    } 

我节省OLDVALUE上PreviewKeyDown事件,并用它在TextChanged事件。

+0

很高兴你解决了:) – tabby

0

您可以使用SelectionStartSelectionLength这样的:

if (searchIndex > Count) 
{ 
    textBox.Text = textBox.Text.Substring(0, textBox.Text.Length - 1); 
    textBox.SelectionStart = textBox.Text.Length -1; 
    textBox.SelectionLength = 0; 
} 
1

试试这个:

代码

private void TextBox_TextChanged(object sender, TextChangedEventArgs e) 
    { 
     TextBox textBox = sender as TextBox; 
     if (textBox != null && !string.IsNullOrEmpty(textBox.Text)) 
     { 
      int searchIndex = 0; 
      int count = 100; 
      int.TryParse(textBox.Text, out searchIndex); 

      if (textBox != null && !string.IsNullOrEmpty(textBox.Text)) 
      { 
       if (searchIndex > count) 
       { 
        textBox.Text = textBox.Text.Substring(0, textBox.Text.Length - 1); 
        textBox.SelectionStart = textBox.Text.Length; 
        textBox.SelectionLength = 0; 
       } 
      } 
     } 
    } 
+0

谢谢。但问题是当我们将光标移动到第一个数字并输入值,然后光标移动到最后一个数字。另一个问题是当我在最后一位数字以外的任何地方输入数值时,则第一位移至第二位,最后一位数字被删除。 – SAT

2

您可以处理PreviewTextInput事件和设置的的Handled财产每当任何时候有TextCompositionEventArgstrue验证失败:

private void SearchTextBox_PreviewTextInput(object sender, TextCompositionEventArgs e) 
{ 
    TextBox textBox = sender as TextBox; 
    if (textBox != null) 
    { 
     string text = textBox.Text + e.Text; 
     if (!string.IsNullOrEmpty(text)) 
     { 
      int searchIndex = 0; 
      int count = 100; 
      int.TryParse(text, out searchIndex); 
      e.Handled = (searchIndex > count); 
     } 
    } 
} 

感谢。你的回答已经解决了我的大部分问题。但是,如果我删除第一位数字并输入另一个数字,那么验证失败。假设数是150。我进入150 &然后删除1 &再次输入1,然后文本框将得到501 &验证将失败

那么,你应该坚持处理TextChanged事件毕竟。试试这个:

private void TextBox_TextChanged(object sender, TextChangedEventArgs e) 
{ 
    TextBox textBox = sender as TextBox; 
    if (textBox != null) 
    { 
     const int count = 100; 
     int searchIndex = 0; 
     int.TryParse(textBox.Text, out searchIndex); 
     if (searchIndex > count) 
     { 
      var textChange = e.Changes.First(); 
      if (textChange != null && textChange.AddedLength > 0) 
      { 
       int caret = textBox.CaretIndex; 
       int length = textBox.Text.Length; 
       textBox.TextChanged -= TextBox_TextChanged; 
       textBox.Text = textBox.Text.Substring(0, textChange.Offset) + textBox.Text.Substring(textChange.Offset + textChange.AddedLength); 
       textBox.TextChanged += TextBox_TextChanged; 
       textBox.CaretIndex = caret - Math.Abs(textBox.Text.Length - length); 
      } 
     } 
    } 
} 
+0

谢谢。你的回答已经解决了我的大部分问题。但是,如果我删除第一位数字并输入另一个数字,那么验证失败。假设计数是150.我输入150然后删除1再次输入1然后文本框将得到501验证将失败。 – SAT

+0

查看我更新的答案。 – mm8

+0

我已经尝试了另一种解决方案并取得了成功。我会尝试你的解决方案,并会接受你的答案。谢谢 !!! – SAT

相关问题