锄头我能否确保用户只能在WPF中的KeyDown事件的文本框中输入字母数字和空格?不允许特殊字符。文本框仅包含字母,数字和空格
2
A
回答
6
检查文本框的keydown事件,做作为
// If escape is presses, then close the window.
if(Key.Escape == e.Key)
{
this.Close();
}
// Allow alphanumeric and space.
if(e.Key >= Key.D0 && e.Key <= Key.D9 ||
e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9 ||
e.Key >= Key.A && e.Key <= Key.Z ||
e.Key == Key.Space)
{
e.Handled = false;
}
else
{
e.Handled = true;
}
// If tab is presses, then the focus must go to the
// next control.
if(e.Key == Key.Tab)
{
e.Handled = false;
}
希望这将帮助......
0
KeyDown事件处理程序可能不是最好的地方,因为该字符已经被添加到TextBox中。您可以响应PreviewKeyDown事件并阻止该事件继续,但这可能会导致未知结果。
一种方法是将ValidationRule附加到TextBox。虽然这不会阻止用户输入角色,但会通知他们他们不被允许。要做到这一点,你从System.Windows.Controls.ValidationRule推导得到的东西是这样的:
public class MyValidationRule : ValidationRule
{
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
// Logic to determine if the TextBox contains a valid string goes here
// Maybe a reg ex that only matches alphanumerics and spaces
if(isValidText)
{
return ValidationResult.ValidResult;
}
else
{
return new ValidationResult(false, "You should only enter an alphanumeric character or space");
}
}
}
,然后用它在XAML这样的:
<TextBox>
<TextBox.Text>
<Binding Path="MyString"
UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<myns:MyValidationRule />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
每次用户输入无效字符他们会得到错误信息。希望这有一些用处。
0
您还可以使用文本框的PreviewTextInput事件。
<TextBox Name="txtName" PreviewTextInput="txtName_PreviewTextInput"/>
并添加代码下面的背后:
private void txtName_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
foreach (char c in e.Text)
{
if (!char.IsLetterOrDigit(c))
{
e.Handled = true;
break;
}
}
}
0
我DONOT认为这是一个很好的做法是使用的KeyDown。 退步是你将不得不处理Backspace,删除和标签键也。
相反: 手柄PreviewTextInput
private void OnPreviewTextInput(object sender, TextCompositionEventArgs e)
{
if (!IsMatch(e.Text)) e.Handled = true;
}
private bool IsMatch(string input)
{
return Regex.IsMatch(input, MyRegexString);
}
则必须照顾PasteHandler的: 添加下面的构造线。
DataObject.AddPastingHandler(TargetTextBoxName, PasteHandler);
private void PasteHandler(object sender, DataObjectPastingEventArgs e)
{
if (!e.DataObject.GetDataPresent(typeof(string))) return;
// Allow pasting only numeric
var pasteText = e.DataObject.GetData(typeof(string)) as string;
if (!IsMatch(pasteText))
{
e.CancelCommand();
}
}
+0
你的正则表达式将是 “^ [A-ZA-Z0-9]” – kkk 2015-12-03 17:39:24
相关问题
- 1. 字母数字是否包含_和空格?
- 2. 记事本++ - 仅匹配仅包含大写字母(或空格)的行
- 3. 文本框只有字母,数字和 -
- 4. PHP编码仅包含字母和数字
- 5. 检查字符串是否仅包含英文字母,数字和符号
- 6. 检查NSString包含字母和数字
- 7. 加载包含数字和字母
- 8. 仅使用字母和空格但不包含空格字符串的Java扫描程序
- 9. Asp.net RegularExpressionValidator仅限数字,字母和空格
- 10. 正则表达式:允许使用字母,数字和空格(至少包含一个字母而非数字)
- 11. 在包含字母和数字的字段中捕获字母
- 12. 文本字段应该只允许字母数字,不仅包括字母和数字?
- 13. 如何验证文本输入仅包含拉丁字母?
- 14. VB.Net验证:检查文本是否仅包含字母
- 15. 如何仅返回包含数字和字母的列中的数字?
- 16. 从包含字母和空格的字符串中解析整数 - C#
- 17. 地带文本框中的所有字符,除了字母和空格
- 18. 实施字母数字文本框
- 19. 只接受字母和空格,数字和空格,以及在不同的框中输入空格的字母和数字
- 20. 自动删除包含“ème”的数字和字母数字文字
- 21. 我如何判断一个字符串是否只包含字母和空格
- 22. 分钟值包含数字,字符和空格
- 23. 返回sql行where字段仅包含非字母数字字符
- 24. 检查一个字符串是否包含数字和字母
- 25. 由包含数字和字母的字段排序
- 26. 分开字包含字母和数字分为两个部分
- 27. 检查密码包含字母,数字和特殊字符
- 28. 使用20个字符后修剪文本框文本,如果包含空格,则使用空格
- 29. Reg Exp:仅匹配数字和空格
- 30. 检查文本字段是否包含javascript中的字母
BTW,处理程序的签名是: 私人无效OnKeyDownHandler(对象发件人,发送KeyEventArgs E) – Jeff 2013-10-31 20:11:01