Search code examples
c#winformsvisibilitymenuitemtoolstrip

How to close TextBox Menu Item properly on KeyPress


I am using radial gauges to display temperatures. The user should be able to rename each gauge, which isn't possible by double-clicking on the name, so I have to make a menu where user can change the names of the gauges.

I solved this by making a menu item for every gauge. So when you click on a menuitem, for example "Thermometer 1", a sub menu of type ToolStripTextBox will appear containing the same text in a TextBox. In that TextBox the user can change the name. I want when the user press Enter in TextBox the textbox menu item get closed, but the parent menu item "Theremometer1" stay open. (Thermometer1, Thermometer2, ... shooul not disappear after closing the textbox menu item.)

Here is the code I use to close that TextBox menu:

private void thermometer1ToolStripMenuItem1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == '\r')
    {
        thermometer1ToolStripMenuItem1.Visible = false;
    }
}

But as you can see small white bar stays visible. Can someone help me please?

How can I close that ToolStripTextBox menu item properly when the user press Enter? That small white bar should not remain visible.

The Menuitems

The small white bar


Solution

  • The Right way to Close a Dropdown

    Setting Visible of the item is not what you need. When you want to close a menu item, it's enough to find it's owner which is ToolStripDropDown and call its Close method:

    private void someToolStripMenuItem_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == (char)Keys.Enter)
        {
            var item = (ToolStripItem)sender;
            var owner = (ToolStripDropDown)(item.Owner);
            owner.Close();
        }
    }
    

    This way you will not face with that small white bar problem, because you close the dropdown. But when instead of closing the dropdown you set the visible of item to false, the dropdown is still open, not containing an element, so it shows as small white bar.

    What's the Small Bar Issue?

    The small white bar which you see is an open ToolStripDropDown containing no item as mentioned above.

    This only occurs when you remove all items of a dropdown while the dropdown is open; in this case the dropdown stays open containing no items. As soon as you close the dropdown, it disappears and will not appear by pointing to its parent.

    If you make all items of a closed dropdown invisible, the dropdown will not appear by pointing to its parent. This behavior is also desired for an open dropdown and the problem needs a workaround.

    You can check if the owner of the item which you made invisible contains no other visible items, then make it also invisible. The owner of the item is that dropdown which stays open.

    private void someToolStripMenuItem_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == (char)Keys.Enter)
        {
            var item = (ToolStripItem)sender;
            item.Visible = false;
            var owner = (ToolStrip)(item.Owner);
            owner.Visible = owner.Items.Cast<ToolStripItem>().Any(x => x.Visible);
        }
    }