鋤頭我能否確保用戶只能在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. Java字符串僅包含數字,空格和符號+ -
- 4. 記事本++ - 僅匹配僅包含大寫字母(或空格)的行
- 5. 僅包含數字的HTML文本
- 6. 文本框只有字母,數字和 -
- 7. 將包含數字和字母的文本行轉換爲int
- 8. 的.htaccess僅重寫,如果路徑包含數字和字母
- 9. PHP編碼僅包含字母和數字
- 10. 如何製作僅接受字母退格和空格的文本字段
- 11. 檢查字符串是否僅包含英文字母,數字和符號
- 12. 加載包含數字和字母2
- 13. 檢查NSString包含字母和數字
- 14. 加載包含數字和字母
- 15. 僅使用字母和空格但不包含空格字符串的Java掃描程序
- 16. Asp.net RegularExpressionValidator僅限數字,字母和空格
- 17. 正則表達式:允許使用字母,數字和空格(至少包含一個字母而非數字)
- 18. 在包含字母和數字的字段中捕獲字母
- 19. 文本字段應該只允許字母數字,不僅包括字母和數字?
- 20. 如何驗證文本輸入僅包含拉丁字母?
- 21. VB.Net驗證:檢查文本是否僅包含字母
- 22. 包含數字和空值
- 23. 如何僅返回包含數字和字母的列中的數字?
- 24. Java:驗證文本字段輸入是否僅包含字母字符
- 25. 檢查字母,數字和空格嗎?
- 26. 從包含字母和空格的字符串中解析整數 - C#
- 27. 的Javascript:驗證字段:沒空和僅包含數字
- 28. 僅提取包含子串字母
- 29. preg_match模式檢查條只包含字母,空格和點
- 30. 只包含字母,空格,連字符和單引號的字符串
BTW,處理程序的簽名是: 私人無效OnKeyDownHandler(對象發件人,發送KeyEventArgs E) – Jeff 2013-10-31 20:11:01