如何检测文本框的 C# keydown 事件中输入的多个按键?
我想在 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;
}
我无法检测到shift和Key.D0到Key.D1,即
SHIFT + 1返回“!”
SHIFT + 3 像任何其他特殊键一样返回“#”。
我不希望用户在文本框中输入特殊字符。
我如何处理这个按键事件?
I want to design a numeric textbox in silverlight.
I have added keydown event of TextBox to handle the keypress.
Inside event I validate the key entered in the textbox.
event as follows
private void TextBox_KeyDown(object sender, KeyEventArgs e)
{
if (!this.Validate(sender,e))
e.Handled = true;
}
function Validate as follows
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;
}
I am not able to detect shift and Key.D0 to Key.D1 i.e.
SHIFT + 1 which returns "!"
SHIFT + 3 returns "#" like wise any other special keys.
I dont want user to enter special character in to textbox.
How do i handle this keys event??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 Silverlight 中,我认为
KeyEventArgs
类中没有任何Modifier
,而是在Keyboard
中:In Silverlight, I don't think there are any
Modifier
s in theKeyEventArgs
class but instead inKeyboard
:e
应该有一个属性,可以告诉您是否按下了 Shift、Control 或 Alt。e.Modifiers
还可以为您提供有关按下了哪些附加修饰键的附加信息。要取消该字符,您可以将
e.Handled
设置为True
,这将导致控件忽略按键。e
should have a property on it that will tell you if Shift, Control or Alt are pressed.e.Modifiers
can also give you additional information about which additional modifier keys have been pressed.To cancel the character you can set
e.Handled
toTrue
, which will cause the control to ignore the keypress.