pra*_*ari 1 c# keyboard silverlight
我想在silverlight中设计一个数字文本框.
我添加了TextBox的keydown事件来处理keypress.
在内部事件中,我验证在文本框中输入的密钥.
事件如下
private void TextBox_KeyDown(object sender, KeyEventArgs e)
{
if (!this.Validate(sender,e))
e.Handled = true;
}
Run Code Online (Sandbox Code Playgroud)
功能验证如下
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;
}
Run Code Online (Sandbox Code Playgroud)
我无法检测到shift和Key.D0到Key.D1 ie
SHIFT + 1返回" ! "
SHIFT + 3像任何其他特殊键一样返回" # ".
我不希望用户在文本框中输入特殊字符.
我该如何处理这个键事件?
在Silverlight中,我认为课堂上没有任何内容Modifier,KeyEventArgs而是在Keyboard:
public void KeyDown(KeyEventArgs e)
{
if (Keyboard.Modifiers == ModifierKeys.Control && e.Key == Key.D0)
{
ControlZero();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4152 次 |
| 最近记录: |