vb.net 在 TextBox 的 KeyDown 处理程序下更改 KeyCode
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12990943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing KeyCode under KeyDown handler of TextBox
提问by Wine Too
I have to rewrite some my old code from VB6 to VB.NET and here is some things I don't know what to do. For example I have to replace some keycodes under keyDown event handler of textbox and I can't do this without help.
我必须将一些旧代码从 VB6 重写为 VB.NET,这里有一些我不知道该怎么做的事情。例如,我必须在文本框的 keyDown 事件处理程序下替换一些键码,没有帮助我无法做到这一点。
Most simple to say, I have workable VB6 code:
最简单的说,我有可用的 VB6 代码:
If KeyCode = vbKeyUp Then
KeyCode = vbKeyEscape
End If
When I try to rewrite this literally:
当我尝试从字面上重写它时:
If e.KeyCode = Keys.Up Then
e.KeyCode = Keys.Escape
End if
But this won't go:
但这不会去:
Error 2 Property 'KeyCode' is 'ReadOnly'.
错误 2 属性“KeyCode”是“只读”。
Since I have much of such conversions to do is here any way to achieve this simple?
由于我有很多这样的转换要做,这里有什么方法可以实现这个简单吗?
回答by JaredPar
The best way to simulate this in VB.Net is to do the following. When you see the Upkey is pressed cancel the event. Then send an artificial key event for the Escapekey.
在 VB.Net 中模拟这一点的最佳方法是执行以下操作。当您看到Up按键被按下时,取消该事件。然后为该Escape键发送一个人工键事件。
Protected Overrides Sub OnKeyDown(ByVal e As KeyEventArgs)
If e.KeyCode = Keys.Up Then
e.Handled = True
SendKeys.Send("ESC")
Else
MyBase.OnKeyDown(e)
End If
End Sub
回答by R.Alonso
Private Sub txtDUnidades_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtDUnidades.KeyPress
If e.KeyChar = "." Then
e.KeyChar = ","
e.Handled = False
End If
End Sub

