[英]How to distinguish between user input, but allow the application itself to change text
I've got a textbox, which does not allow users to enter "[" and "]" characters. 我有一个文本框,不允许用户输入“ [”和“]”字符。 However, I would like to set the text to be a value containing both "[" and "]" by default.
但是,我想将文本默认设置为同时包含“ [”和“]”的值。 How can I go about this?
我该怎么办? This is my code:
这是我的代码:
TextBox txtEdit = new TextBox();
txtEdit.TextChanged += txtEdit_TextChanged;
txtEdit.Text = "[Test]";
private void txtEdit_TextChanged(object sender, TextChangedEventArgs e)
{
if ((sender as TextBox).Text.Contains("[") || (sender as TextBox).Text.Contains("]"))
{
MessageBox.Show("Cannot enter '[' or ']' characters!", "", MessageBoxButton.OK, MessageBoxImage.Information);
(sender as TextBox).Text = "";
}
}
Put the TextChanged
Event after you change the Text
property 更改
Text
属性后放置TextChanged
事件
TextBox txtEdit = new TextBox();
txtEdit.TextChanged += txtEdit_TextChanged;
private void txtEdit_TextChanged(object sender, TextChangedEventArgs e)
{
if (comboBoxYou.Text == txtEdit.Text)
{
return;
}
else if ((sender as TextBox).Text.Contains("[") || (sender as TextBox).Text.Contains("]"))
{
MessageBox.Show("Cannot enter '[' or ']' characters!", "", MessageBoxButton.OK, MessageBoxImage.Information);
(sender as TextBox).Text = "";
}
}
I'd set a keydown
event for the Textbox. 我将为文本框设置一个
keydown
事件。
If you don't want users enetering the '[' and ']' characters, then read in the key character and set e.Handled = true
if it matches the character you don't want. 如果您不希望用户输入'['和']'字符,请读入关键字符并设置
e.Handled = true
如果它与您不需要的字符匹配)。
The answer of Toon Casteele is a good solution. Toon Casteele的答案是一个很好的解决方案。 Otherwise try adding a boolean to your form and when the user selects a value set it to true and check for the value in the TextChanged event if it is true.
否则,请尝试向表单中添加一个布尔值,当用户选择一个值时,将其设置为true,然后检查TextChanged事件中的值是否为true。
private bool IsSelectedItemText; //set it to true when an item is selected
And then do following check: 然后执行以下检查:
if(!IsSelectedItemText)
//Check for square brackets
else
//Add text from item
Also, and this is not directly related to the question but I strongly suggest that you cast the 'sender' object only once. 另外,这与问题没有直接关系,但我强烈建议您仅将“ sender”对象强制转换一次。 Complete, the code would look like:
完成后,代码如下所示:
private void txtEdit_TextChanged(object sender, TextChangedEventArgs e)
{
TextBox tb = sender as TextBox;
if(!IsSelectedItemText)
{
if (tb.Text.Contains("[") || tb.Text.Contains("]"))
{
MessageBox.Show("Cannot enter '[' or ']' characters!", "", MessageBoxButton.OK, MessageBoxImage.Information);
tb.Text = "";
}
}
else
{
//Set text from selected item
}
IsSelectedItemText = false;
}
Remember to set the boolean-variable to 'true' when an item is selected. 请记住,选择一个项目时,将布尔变量设置为“ true”。
If thats what you wanted. 如果那是您想要的。
private void txtEdit_TextChanged(object sender, TextChangedEventArgs e)
{
string a = (sender as TextBox).Text.substing(0,1).subsctring;
a = a.Remove(0, 1);
a = a.Remove(a.Length - 1, 1);
if (a.Contains('[') || a.Contains(']') )
{
MessageBox.Show("Cannot enter '[' or ']' characters!", "", MessageBoxButton.OK, MessageBoxImage.Information);
}
(sender as TextBox).Text = "[" + a.Replace("[", "").Replace("]", "") + "]";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.