ContextMenuStrip prevents keys from working in custom control!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • WebDunce
    New Member
    • Mar 2007
    • 15

    ContextMenuStrip prevents keys from working in custom control!

    hey all,

    i'm building a custom control. it contains a treeview and a context menu. each tree node's context menu property is set to the control's context menu.

    it works good...except.. .

    if after right-clicking on a tree node, if you decide NOT to use any of the options and click OFF of the tree view...the context menu disappears (great!) but then the control no longer seems to process key strokes. it's like the context menu, tho no longer having focus, is still catching the key strokes.

    if i right-click on another node and select an option from the context menu...the keys start working again.

    any ideas on how to get the keys working if i pull up the context menu but do NOT select an option?

    i feel like the answer to this puzzle must be very simple, for some reason...like a property on the context menu i need to set...or an event i need to handle.

    thanks all.
  • WebDunce
    New Member
    • Mar 2007
    • 15

    #2
    further testing confirms that indeed if a menu item on the context menu is selected, my custom control regains control of key input, but if i simply click off of the context menu, then the context menu retains control of the key input.

    I tested this by overriding the ProcessCmdKey sub in the context menu object. i had it display a messagebox whenever it fired. While the menu displays, it fires. If a menu item is selected, the menu disappears and my custom control can use the keys normally. but if i simply click off the menu, the menu does disappear but key strokes continue to cause the context menu's ProcessCmdKey sub to fire.

    Whether i click on or off the menu, in both cases it disappears and loses focus (confirmed by messagebox)...s o i cant see why it is retaining control of the keys!

    I've looked at so much of microsoft's documentation on the ContextMenuStri p, but nothing seems to address this issue directly (i.e., nothing says: "caution: if you dont do xxxxx, then your contextmenustri p will not release control of the keyboard input if the user fails to select an item, and this will annoy you to no end...")

    pressing escape, while the context menu is visible -- or after clicking off it -- will return control of keyboard input to my custom control.

    Comment

    • WebDunce
      New Member
      • Mar 2007
      • 15

      #3
      okay, i've reconstructed a highly simplified version of my program...a form, a usercontrol with a treeview and nodes, and a context menu....

      the context menu is working fine on the simplified version...that let's me know the issue is with my code...not with the .net objects themselves or any of their properties...of course there's no way i'd expect anyone to find the problem in my code...i wouldn't even know what portions to post...but at least i no longer think i am overlooking some property or setting or event or whatnot on the context menu (nor am i any longer considering that the context menu control is buggy)...

      if ever i find the problem i'll try to remember to post the answer...i'm sure everyone is just dying to find out. ;)

      Comment

      • kenobewan
        Recognized Expert Specialist
        • Dec 2006
        • 4871

        #4
        I'll be interested. Don't know how to solve your problem but simplifying and debugging is the right way to go.

        Comment

        • WebDunce
          New Member
          • Mar 2007
          • 15

          #5
          Originally posted by kenobewan
          I'll be interested. Don't know how to solve your problem but simplifying and debugging is the right way to go.
          hi kenobewan,

          thanks for your interest. i have found the problem!

          right-clicking on any of my treeview's tree-nodes fired the sub below...

          Code:
          Private Sub tvwMenu_NodeMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles tvwMenu.NodeMouseClick
                  ' if we click the right mouse button...
                  If e.Button = Windows.Forms.MouseButtons.Right Then
          
                      ' a bunch of statements which define
                      ' the options that should appear
                      ' on the ContextMenuStrip
          
                      ' show the context menu
                      e.Node.ContextMenuStrip.Visible = True
                  End If
              End Sub
          the problem is the line:
          Code:
          e.Node.ContextMenuStrip.Visible = True
          apparently the contextmenu knows it is supposed to show itself on the right-click of the associated object...so not only is the statement simply uneccessary...i t also, for some unknown reason, causes my issue. when this line is removed...all is well.

          good bit of detective work there...hours and hours and hours...over such a small thing!

          --William

          Comment

          • WebDunce
            New Member
            • Mar 2007
            • 15

            #6
            on the bright side...it is not the complexity of my code in general but is actually what i had originally thought...my misuse of the ContextMenuStri p object.

            Comment

            Working...