C# 在不干扰光标位置的情况下,将文本框输入限制为特定值
我有一个WPF应用程序(MVVM)。我想限制用户在文本框中输入超过特定值。 假设该值为“100”,则用户不应该能够输入101等。我已经尝试了以下代码 XAML:C# 在不干扰光标位置的情况下,将文本框输入限制为特定值,c#,wpf,mvvm,textbox,cursor,C#,Wpf,Mvvm,Textbox,Cursor,我有一个WPF应用程序(MVVM)。我想限制用户在文本框中输入超过特定值。 假设该值为“100”,则用户不应该能够输入101等。我已经尝试了以下代码 XAML: 有了这段代码,我可以限制用户输入超过特定的值。但问题是,当我设置TextBox的文本时,光标会移到第一位。有什么解决方法吗?您可以像这样使用SelectionStart和SelectionLength: if (searchIndex > Count) { textBox.Text = textBox.Text.Subs
有了这段代码,我可以限制用户输入超过特定的值。但问题是,当我设置TextBox的文本时,光标会移到第一位。有什么解决方法吗?您可以像这样使用
SelectionStart
和SelectionLength
:
if (searchIndex > Count)
{
textBox.Text = textBox.Text.Substring(0, textBox.Text.Length - 1);
textBox.SelectionStart = textBox.Text.Length -1;
textBox.SelectionLength = 0;
}
您可以像这样使用
SelectionStart
和SelectionLength
:
if (searchIndex > Count)
{
textBox.Text = textBox.Text.Substring(0, textBox.Text.Length - 1);
textBox.SelectionStart = textBox.Text.Length -1;
textBox.SelectionLength = 0;
}
试试这个:
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);
}
}
}
}
代码
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;
}
}
}
}
试试这个:
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);
}
}
}
}
代码
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;
}
}
}
}
当验证失败时,您可以处理
previewtextireput
事件,并将TextCompositionEventArgs
的Handled
属性设置为true
:
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);
}
}
}
}
当验证失败时,您可以处理
previewtextireput
事件,并将TextCompositionEventArgs
的Handled
属性设置为true
:
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);
}
}
}
}
&。在这两个答案的帮助下,我解决了我的问题。我正在处理两个事件PreviewKeyDown和TextChanged
代码:
我正在PreviewKeyDown事件中保存旧值,并在TextChanged事件中使用它。&。在这两个答案的帮助下,我解决了我的问题。我正在处理两个事件PreviewKeyDown和TextChanged
代码:
我正在PreviewKeyDown事件中保存旧值,并在TextChanged事件中使用它。最好为wpfbetter查找NumericUpDown控件,或为wpfThanks查找NumericUpDown控件。但问题是,当我们将光标移动到第一位并输入值时,光标就会移动到最后一位。另一个问题是,当我在最后一位以外的任何位置输入值时,第一位移到第二位,最后一位被删除。谢谢。但问题是,当我们将光标移动到第一位并输入值时,光标就会移动到最后一位。另一个问题是,当我在最后一位以外的任何位置输入值时,第一位移到第二位,最后一位被删除。谢谢。你的回答解决了我大部分的问题。但如果我删除第一个数字并输入另一个数字,则验证失败。假设计数是150。我输入150,然后删除1,再次输入1,文本框将获得501,验证将失败。我尝试了另一个解决方案并获得成功。我会尝试你的解决方案,并接受你的答案。谢谢谢谢你的回答解决了我大部分的问题。但如果我删除第一个数字并输入另一个数字,则验证失败。假设计数是150。我输入150,然后删除1,再次输入1,文本框将获得501,验证将失败。我尝试了另一个解决方案并获得成功。我会尝试你的解决方案,并接受你的答案。谢谢