在 TextBox 的 KeyDown 处理程序下更改 KeyCode [英] Changing KeyCode under KeyDown handler of TextBox

查看:24
本文介绍了在 TextBox 的 KeyDown 处理程序下更改 KeyCode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须将一些旧代码从 VB6 重写为 VB.NET,这里有一些我不知道该怎么做的事情.例如,我必须在文本框的 keyDown 事件处理程序下替换一些键码,没有帮助我无法做到这一点.

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 代码:

Most simple to say, I have workable VB6 code:

If KeyCode = vbKeyUp Then
   KeyCode = vbKeyEscape
End If

当我尝试逐字改写时:

If e.KeyCode = Keys.Up Then
   e.KeyCode = Keys.Escape
End if

但这不会发生:

错误 2 属性KeyCode"是只读".

Error 2 Property 'KeyCode' is 'ReadOnly'.

既然我有很多这样的转换要做,那么这里有什么方法可以实现这个简单的吗?

Since I have much of such conversions to do is here any way to achieve this simple?

推荐答案

在 VB.Net 中模拟此问题的最佳方法是执行以下操作.当您看到 Up 键被按下时,取消该事件.然后为 Escape 键发送一个人工键事件.

The best way to simulate this in VB.Net is to do the following. When you see the Up key is pressed cancel the event. Then send an artificial key event for the Escape key.

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

这篇关于在 TextBox 的 KeyDown 处理程序下更改 KeyCode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆