Dismiss a menu programmatically?

cjard

Well-known member
Joined
Apr 25, 2006
Messages
7,081
Programming Experience
10+
I have a menu with a textbox as one og the items. When the user types an client reference number into the box, and presses return, i load a form to show the client details..

How can i get the menu to go away properly?

so far i have:
VB.NET:
    Private Sub JumpToClientNumber_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles JumpToClientNumber.KeyPress

        If e.KeyChar = returnChar Then
            frmClientDetail.MdiParent = Me
            frmClientDetail.ShowClient(JumpToClientNumber.Text)
            frmClientDetail.Show()
            JumpToClientNumber.Text = JumpToClientNumber.Tag
            ClientSetupToolStripMenuItem.HideDropDown()
            frmClientDetail.Focus()
        End If
    End Sub
returnChar is worked out in advance by Convert.ToChar(Keys.Return)
the form is a mdi desktop that makes the client form an mdi child and loads the client data
it then shows the form..
..and hides the menu dropdown
..and focuses the child form


but the snag here is the menu is still active to mouseovers. if i point to the menu bar without clicking anything a menu will appear.. how can i close the menu properly, like clicking a menu item would?
 
Thats how menu's work by design. They work on the principle of a menu loop that runs when you click an item and then checks for events both inside and outside it's client area. The only way i can think of is to use the sendmessage api to send a click message to the menu and trick it into thinking that it has been decativated.
 
cjard, vis781 is right, that's how menus work, when you click an item the parent dropdown is hidden and menu is 'dismissed', and this is something you can take advantage of. I gather you don't have an click handler also for this ToolStripTextBox, so there should be no interfering code being performed. Instead of ClientSetupToolStripMenuItem.HideDropDown() you code JumpToClientNumber.PerformClick() which will invoke the default click behaviour of the menu, ie. hide the parent dropdown and also 'dismiss' the menu for hovers as requested. Also note that there don't have to exist an actual Click handler for that control to call the PerformClick method, it will work nevertheless.
 
hey guys.. im having some difficulty following the advice given here - it doesnt seem to work as expected.

it may be worthy of note that the textboxes themselves cause a strange behaviour with click, when used in a menu. i never mentioned that my textbox has a click handler already- it checks to see if the current textbox text is equal a default and if so, then it selects all

this way a textbox can be self-labelling, but it seems that some other things stop working. if i call PerformClick as suggested, then the click handler is run, but nothing changes- the menus are still visible and sensitive to mouseovers (popping the menu down). i cant see a difference with calling performclick. in order to check it wasnt the existence of the click handler, i arranged the same thing on another menu and it suffers the same issues..

any further suggestions? tia

VB.NET:
[SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] JumpToClientNumber_KeyPress([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Windows.Forms.KeyPressEventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] JumpToClientNumber.KeyPress
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] e.KeyChar = returnChar [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]ClientSetupToolStripMenuItem.HideDropDown()
ClientSetupToolStripMenuItem.PerformClick()
frmLogin.MdiParent = [/SIZE][SIZE=2][COLOR=#0000ff]Me
[/COLOR][/SIZE][SIZE=2]frmLogin.Show()
[/SIZE][SIZE=2][COLOR=#008000]'frmClientDetail.MdiParent = Me
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'frmClientDetail.ShowClient(JumpToClientNumber.Text)
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'frmClientDetail.Show()
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'JumpToClientNumber.Text = JumpToClientNumber.Tag
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'frmClientDetail.Focus()
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] JumpToClientNumber_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] JumpToClientNumber.Click
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] JumpToClientNumber.Text = JumpToClientNumber.Tag [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE][SIZE=2] JumpToClientNumber.SelectAll()
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] JumpToClientNumber_DoubleClick([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] JumpToClientNumber.DoubleClick
JumpToClientNumber.SelectAll()
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
 
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] ToolStripTextBox1_KeyPress([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Windows.Forms.KeyPressEventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] ToolStripTextBox1.KeyPress
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] e.KeyChar = returnChar [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]CustomerToolStripMenuItem.HideDropDown()
ToolStripTextBox1.PerformClick()
frmLogin.MdiParent = [/SIZE][SIZE=2][COLOR=#0000ff]Me
[/COLOR][/SIZE][SIZE=2]frmLogin.Show()
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]
(sorry for the lack of indent- my laptop wont go into basic editor mode like my old laptop did)
[/COLOR][/SIZE]
 
cjard said:
hey guys.. im having some difficulty following the advice given here - it doesnt seem to work as expected.

any further suggestions? tia
Yes, you have to really follow this advice:
JohnH said:
Instead of ClientSetupToolStripMenuItem.HideDropDown() you code JumpToClientNumber.PerformClick()
That means that you have remove the line that says ClientSetupToolStripMenuItem.HideDropDown() and instead place the line that says JumpToClientNumber.PerformClick().

To sum it up: The error you did was to leave the problem code (HideDropDown) and also do the remedy (PerformClick) on the wrong control (the parent). On your other try you actually PerformClick on the correct control (the ToolStripTextBox), but doing HideDropDown first does something wrong to the menu in this regard.
 
i thought i'd done those suggestions, but it seems i did not.. when i tried them as you said, they worked perfectly. many thanks for your help! :)
 
Back
Top