Method to determine if Mnemonic/Accelerator key values apply to a control?

1k views Asked by At

I have an application (.net framework 4, vb.net) that originally was a VB6 application. To mimic portions of the old tab control behavior, I am implementing accelerator keys that allow you to switch to another tab. Example - TabControl with 5 tabs. - Tab 2 has a label &Data (alt-d accelerator) with a textbox - User has Tab 1 selected, and hits alt-d which causes the tab control to select tab 2 and set focus to the corresponding textbox.

I have written some code which looks for the tab which contains the control (I do this by overriding ProcessMnemonic) and simply look through the tabs (starting with the selected one) and if I find a match, I select the tab then allow the system to process the mnemonic by calling "MyBase.ProcessMnemonic(charCode)".

But my issue is the Control.IsMnemonic call. Since you only pass the control's "Text" any control which contains an & in it's text property can cause it to be a match.

For instance, myTextbox.Text = "here &friend" would cause Alt-F to set focus on that box.

I can explicitly check if the control type is a label... but then I also need groupboxes and... whatelse? Buttons I should also allow the mnemonic...

Here's some code (note I didn't include the tab iteration as it didn't seem pertinent);

Private Function IsMnemonicInThisContainer(charCode As Char, controlContainer As System.Windows.Forms.Control.ControlCollection) As Boolean

    For Each ctrl As Control In controlContainer

        If Control.IsMnemonic(charCode, ctrl.Text) Then

            If ControlIsAlive(ctrl) Then
                Return True
            End If

        ElseIf ctrl.HasChildren Then

            If ControlIsAlive(ctrl) AndAlso IsMnemonicInThisContainer(charCode, ctrl.Controls) Then
                Return True
            End If

        End If
    Next

    Return False

End Function

Private Function ControlIsAlive(ctrl As Control) As Boolean

    ' In a TABPAGE that is not selected, the controls all appear to be visible = FALSE,
    ' because they aren't actually "visible" - HOWEVER... the control itself may be expecting
    ' to be visible (once it's tab is shown)... so this call to GetStateMethodInfo which I grabbed from
    ' http://stackoverflow.com/questions/3351371/using-control-visible-returns-false-if-its-on-a-tab-page-that-is-not-selected
    ' is the solution I needed. 
    ' Instead of walking the tree though I am going to "check containers" as I drop into them... if they are not enabled/visible
    ' then I'm not going to go any deeper

    ' Is control enabled and going to be shown?  (calling ctrl.visible allows us to bypass the other call if we can!)
    Return (ctrl.Enabled AndAlso (ctrl.Visible OrElse CBool(GetStateMethodInfo.Invoke(ctrl, New Object() {2}))))

End Function

I suppose I could do something like...

If Typeof ctrl is Label orelse Typeof ctrl is groupbox (etc...)...

But a property (or method) to determine this would be great. Any ideas?

Thanks! Chris Woodruff

0

There are 0 answers