Link to home
Start Free TrialLog in
Avatar of Dabas
DabasFlag for Australia

asked on

Hide a context menu

When right clicking a button, I create a context menu dynamically, filling its values from a dataset

The program works fine, that is not the problem.
What happens if the user does not want to choose any of the values and wants the menu to "hide" itself from view?

Currently the user has to click elsewhere to achieve this effect.
I have been asked to accomplish this whenever the mouse leaves the context menu's area.

1) Can this be done? (There is no MouseLeave event for the context menu control)
2) How?

This is my code to create the menu:

  Private Sub btnMenu_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        Try
            If e.Button = MouseButtons.Right Then
                Dim mnu As New ContextMenu
                For Each dr As DataRow In dsValues.Tables(0).Rows
                    Dim mi As New MenuItem
                    mi.Text = dr.Item("Symbol").ToString
                    mnu.MenuItems.Add(mi)
                    AddHandler mi.Click, AddressOf ProcessMenuItem
                Next
                mnu.Show(CType(sender, Control), New Point(-20, -20))
            End If
        Catch ex As Exception
            GeneralErrors(ex)
        End Try
    End Sub
Avatar of gregoryyoung
gregoryyoung
Flag of Canada image

This is intended functionality of the menu ... same as how regular menus work.

If the user slips a bit especially with sub menus it would kind of suck to have the menus disappear. I'm sure there is a way you could work around it by watching where the mouse is on the screen and forcing it to close but you would then be leaving a standard windows interface.
Avatar of Dabas

ASKER

Greg:
Thanks for your reply!

> by watching where the mouse is on the screen
Tried that. I do not seem to get any mouse events to trigger while the menu is up.

Dabas
I mean watch global events via API
Avatar of Dabas

ASKER

UGH! I don't like to use API calls, LOL!
ASKER CERTIFIED SOLUTION
Avatar of gregoryyoung
gregoryyoung
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tovvenki
tovvenki

Hi,
what about using timers and closing the contextmenu after a time interval

Private Sub timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs)
 
     SendKeys.Send("{ESC}")
 
     timer1.Stop()
 
End Sub
 
Private Sub contextMenu1_Popup(ByVal sender As Object, ByVal e As System.EventArgs)
 
     'set interval to 5 seconds
 
     timer1.Interval = 5000
 
     timer1.Start()
 
End Sub
 
Regards,
Venki
Avatar of Dabas

ASKER

Venki:
Thanks for the idea!
Not really what I am after, though.
This might be quite frustrating to the user when he moves the arrow away from the menu on second two and does not understand why it suddenly dissappears a full three seconds later when he was about to do something else. Or alternatively, a user who is hesitating and just as he is about to click on an option, the whole thing dissappears!

Dabas
SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Dabas

ASKER

Thanks Bob.
Yours and Greg's opinion only reinforced whatever I thought to start with, but I was asked to "- When right click a button to get pull down ..if move mouse away, then get rid of pull down.", and did not want to go back with "This is too much trouble for something nobody will be using anyway!", without checking that this is really the case.

Dabas