【发布时间】:2011-10-31 07:00:05
【问题描述】:
我想在 silverlight 中设计一个数字文本框。
我已经添加了 TextBox 的 keydown 事件 来处理按键。
内部事件我验证在文本框中输入的密钥。
事件如下
private void TextBox_KeyDown(object sender, KeyEventArgs e)
{
if (!this.Validate(sender,e))
e.Handled = true;
}
函数验证如下
private bool Validate(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter) //accept enter and tab
{
return true;
}
if (e.Key == Key.Tab)
{
return true;
}
if (e.Key < Key.D0 || e.Key > Key.D9) //accept number on alphnumeric key
if (e.Key < Key.NumPad0 || e.Key > Key.NumPad9) //accept number fomr NumPad
if (e.Key != Key.Back) //accept backspace
return false;
return true;
}
我无法检测到移位和 Key.D0 到 Key.D1 即
SHIFT + 1 返回 "!"
SHIFT + 3 与任何其他特殊键一样返回“#”。
我不希望用户在文本框中输入特殊字符。
如何处理这个按键事件??
【问题讨论】:
标签: c# silverlight keyboard