2017-08-17 24 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

相關問題