Nilbert
Nilbert

Reputation: 1132

Closing a MenuStrip programmatically

I have a MenuStrip that I have added to a form, and in one of the dropdown menus in it, I have a text box. When I hit enter on the textbox, I want a function to run, then the drop down menu to close. I know how to get the enter part done, but I have no idea how to close the MenuStrip dropdown menu.

Upvotes: 4

Views: 8870

Answers (3)

JuanR
JuanR

Reputation: 7783

This is an old question, but I ran into the same issue and figured out the solution, so for others out there:

You need to call the HideDropDown() method of the main menu item, regardless of how nested your textbox (or other control) is.

For instance, let's say you have a tool strip with File, Edit, Help. On the Edit menu, you have your textbox nested somewhere:

EditMenuItem -> FindMenuItem -> SearchTextBoxHere

You would call the Edit menu's HideDropDown() method on your textbox's keydown event:

EditMenuItem.HideDropDown();

Upvotes: 1

WereWolf
WereWolf

Reputation: 31

You can try this (worked for me)

for(int x = 0; x < menu.Items.Count; x++) ((System.Windows.Forms.ToolStripDropDownItem)menu.Items[x]).HideDropDown();

Upvotes: 3

Hans Passant
Hans Passant

Reputation: 941397

Call the Owner's Hide() method. For example:

    private void toolStripTextBox1_KeyDown(object sender, KeyEventArgs e) {
        if (e.KeyData == Keys.Enter) {
            e.SuppressKeyPress = true;
            toolStripTextBox1.Owner.Hide();
        }
    }

Upvotes: 10

Related Questions