Closing a MenuStrip programmatically

Nilbert picture Nilbert · May 24, 2010 · Viewed 7.6k times · Source

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.

Answer

Hans Passant picture Hans Passant · May 24, 2010

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();
        }
    }