• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 426
  • Last Modified:

Function keys assigned to ToolStrip items don't respond properly

Hi guys,
I'm having problems with function keys assigned to ToolStrip items.

I was expecting to receive an event every time I press a function key (F2-F12) but instead I have to press the same function key again before another function key will respond to a key press.

Any ideas why?

/Michael
0
gem56
Asked:
gem56
  • 5
  • 4
1 Solution
 
Bob LearnedCommented:
I don't have any ideas, since they would be just shots in the dark, and somebody could get hurt.

Bob
0
 
gem56Author Commented:
Fair enough. ;-)
0
 
Bob LearnedCommented:
My friend, that was your opportunity to explain how you assigned the function keys, and how you are attaching event handlers to the button clicks.

Bob
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
gem56Author Commented:
It was getting a bit late here, sorry.

I tried two ways. The first was setting the ShortcutKeys property in Properties panel, with ShowShortcutKeys also set, making the (F5) setting appear in the ToolStripMenuItem.

The second method was using the following statement in the Form_Load function:
        Me.TraceOffTraceToolStripButton.ShortcutKeys = Keys.F5
        Me.TraceOnTraceToolStripButton.ShortcutKeys = Keys.F6

That too made F5 appear in the menu item and pressing it the fist time it triggered the 'TraceOffTraceToolStripButton_Click' event handler but pressing F6 it would not fire event to TraceOnTraceToolStripButton_Click handler until I pressed F5 again and then on pressing F6 it would raise an event. It give me a toggling type of action.

I've used function keys in menu bar in VB6 without problems so I'm a bit confused here as to why so fussy!

Hope hat helps.

P.S. I'm sure it will be something absoluttely silly on my part but I can't see it now.
0
 
Bob LearnedCommented:
1) What is the value for Form.KeyPreview?

2) What kind of ToolStripButton are you working with?

3) It might be some property on the button that is messing it up, but that are a few properties to check.

4) I haven't seen this problem before, so it might just be a misunderstanding about a property.

Bob

0
 
gem56Author Commented:
I tried setting Me.KeyPreview in the Form to True but that didn't make any difference.
I'm just using the standard ToolStrip control from the Toolbox.
I've had a good look at the button properties and can't see anything else related to shortcuts other than ShortcutKeys , ShowShortcutKeys and ShortcutKeyDisplayString of which I haven't set ShortcutKeyDisplayString to anything.

I just did a bit more experimenting with the following results.
On entry to the event handler I also call TraceToolStripButton_DropDownOpening function that adjusts Enabled state for each menu item in the list. That way user/me can't press an item that is not applicable. It seems that calling that (_DropDownOpening) function does something. Maybe having pressed F4 (for example) if the _DropDownOpening function changes the Enabled state it screws things up. I've included functions below and I'll also take a closer look at both now.

    Private Sub TraceOffTraceToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TraceOffTraceToolStripButton.Click
        TraceToolStripButton_DropDownOpening(Me.TraceToolStripButton, e)
        If CType(sender, ToolStripMenuItem).Enabled = True Then
            Me.Main.SystemOptions.TraceEnable = False
            Me.Main.MyDiagnostics.SetTraceEnable(Me.Main.SystemOptions.TraceEnable)
            AdjustStatusBarLabels()
        Else
            Beep()
        End If
    End Sub

    Private Sub TraceToolStripButton_DropDownOpening(ByVal sender As Object, ByVal e As System.EventArgs) Handles TraceToolStripButton.DropDownOpening
        If Me.Main.MyDiagnostics.Busy = False Then
            Dim objMenuItem As ToolStripSplitButton = CType(sender, ToolStripSplitButton)
            For Each Item As ToolStripItem In objMenuItem.DropDownItems
                If InStr("ToolStripSeparator", Replace(Item.Name, sender.name, "")) > 0 Then Item.Enabled = False
                If InStr("TraceOff,SetTraceMarker", Replace(Item.Name, sender.name, "")) > 0 Then Item.Enabled = Me.Main.SystemOptions.TraceEnable
                If InStr("TraceOn", Replace(Item.Name, sender.name, "")) > 0 Then Item.Enabled = Not Me.Main.SystemOptions.TraceEnable
                If InStr("ResetTraceFile,ShowTrace,OpenTraceViewer", Replace(Item.Name, sender.name, "")) > 0 Then Item.Enabled = True
            Next Item
        End If
    End Sub

0
 
Bob LearnedCommented:
If you are changing the Enabled state for menu items or ToolStrip items, the results can often be unpredictable.  I can't quite see what effect you are try achieve, so it is difficult to make any suggestions.  What are the rules that dictate whether an item is enabled/disabled?  It might be set at a better place, if I can understand your requirements.

Bob
0
 
gem56Author Commented:
Basically I wanted to make the drop-down list context sensitive. I then thought the simplest way would be to adjust Enabled state when user presses ToolStripSplitButton, and that works fine when that button is being pressed to get to the actual item list.

Of course having set up the function keys throws the spanner into the works because that mans not pressing ToolStripSplitButton and therefore not executing TraceToolStripButton_DropDownOpening function resulting in the list items Enabled states being as per the last time that I pressed TraceToolStripButton.

I can do it differently of course (such as determining 'Enabled' state without actually setting it when a function key is pressed) but I didn't see any obvious problems doing it this way, especially with the normal way (pressing the ToolStripSplitButton) working fine.

/Michael
0
 
gem56Author Commented:
Hi Bob,
You're right about Enabled state getting confused.

The way I solved the problem was to always set Enabled state to True upon exit from the (button handling) function. That meant the button was OK for next time otherwise it (sometimes) was left in disabled state and of course that means it wasn't going to respond to function key presses.

Thanks (till next time) ;-)
     Michael
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now