Alt Codes Do Not Move From User Control

In our main form of processing, we use alt codes (Having & Label as the label text to quickly go to the control, for example, using Alt-L) to quickly navigate the jumps on the screen if the user needs to exit the traditional navigation stream - but lately we have been trapped:

We have a user control that processes a bit in a form in which there is a control inside it, to which you need to pass the alt code (Alt-R) with another control outside the user control. Usually this will not be a problem, since we could just set both of them in Alt-R, and Alt-R will switch between them. Since one of the controls is inside the user control and one of them is not, however, when the focus is inside the user control, it will not switch outside the user control with two controls sharing Alt code.

Is there any property that I can set to resolve this without writing custom logic? The main problem with user logic is that some of these alt codes can be user defined, and it would be impractical to write an all-embracing processing method if I believe that this should work only in the regular Windows Forms Engine

+4
source share
1 answer

I could not find another solution than using P / Invoke for this.
If this does not fit the pattern, there may be an alternative trivial way.

: keydown. KeyDown , "" WindowProc.
: , keydown. - , PostMessage.

dll:

Imports System.Runtime.InteropServices

<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function PostMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Boolean
End Function

ProcessCmdKey UserControl:

Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
  'If ALT has been pressed...
  If (keyData And Keys.Alt) > 0 Then
     '... if another key is pressed aswell...
     If (keyData Xor Keys.Alt) > 64 Then
        '...pass the information to the container to see if it is interested
        PostMessage(Me.ParentForm.Handle, CType(msg.Msg, UInt32), msg.WParam, msg.LParam)
        Return True
     End If
  End If
  'Not a key we are interested in
  Return MyBase.ProcessCmdKey(msg, keyData)
End Function

/ P/Invoke, , .
, PostMessage :

Me.ParentForm.Controls(0).Focus()

, , ALT/key.

+3

Source: https://habr.com/ru/post/1690333/


All Articles