Search code examples
c#windowsformsmenustrip

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.


Solution

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