Windows Forms custom control: focus and cursor keys without UIPermissionWindow.AllWindows

倾然丶 夕夏残阳落幕 提交于 2019-12-25 01:44:44

问题


I want to write a custom control (a text editor) for Windows Forms, which should include the following functionality:

  • Gets the keyboard focus, when you click on it with the mouse
  • Sees all keyboard input (including cursor keys), when it has the focus,
  • Can run in a semi-trusted environment, with UIPermissionWindow.SafeTopLevelWindows (i.e. it shouldn't require UIPermissionWindow.AllWindows)

Is there any example of doing this?

Some of the methods which I might want to use, like Control.Focus() and Control.InInputKey(), require UIPermissionWindow.AllWindows.

Is there any other way to get/implement the functionality, without using these methods?

The built-in TextBox control has this functionality (gets the focus and handles cursor keys).


回答1:


Public Class UserControl1
    Inherits TextBox

    Private Sub UserControl1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.GotFocus

    End Sub

    Private Sub UserControl1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        Debug.WriteLine("downed")
        Debug.WriteLine(e.KeyValue)
        Debug.WriteLine(e.KeyCode)

    End Sub

    Private Sub UserControl1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
        Debug.WriteLine("pressed")
        Debug.WriteLine(e.KeyChar)
    End Sub
End Class


来源:https://stackoverflow.com/questions/3608652/windows-forms-custom-control-focus-and-cursor-keys-without-uipermissionwindow-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!