1

I use a button to show context menu.

bool mnuOpen=false;
private void btnQLDT_Click(object sender, EventArgs e)
{
    if (mnuOpen)
    {
        mnu.Hide();
        mnuOpen = false;
    }
    else
    {
        mnu.Show(btnQLDT, new Point(0, btnQLDT.Height));
        mnuOpen = true;
    }
}

I want when click the button again, this menu will be hide. I try use a bool flag, but when click another space in form. It take 2 click to show menu again.

Problem at my second click not in button area, mnuOpen dont set false . How i can set false when menu close? ContextMenu have Closed Event, but I dont know how to use it. I try set mnuOpen =false in this Event but fail :(

Please help me. Thank in advance!

Ok. I sloved this problem. Here: C# Winfoms Toolstripdropdown close on button click

Community
  • 1
  • 1
  • 1
    " I try use a bool flag" I don't see any – Mong Zhu May 11 '17 at 10:48
  • I updated bro :) – Lê Trung May 11 '17 at 10:57
  • do you change `mnuOpen` anywhere else in the code? is the menu visible at the start of the program? – Mong Zhu May 11 '17 at 11:13
  • Did you debug? what is the value of `mnuOpen` in the first time you click? – Ofir Winegarten May 11 '17 at 11:15
  • Start program `mnuOpen =false` .When I frist click, menu will show, `mnuOpen =true`, and click again, menu will Hide and `mnuOpen =false` .... – Lê Trung May 11 '17 at 11:26
  • Problem at my second click not in button area, `mnuOpen` dont set `false` . How i can set `false` when menu close? ContextMenu have Closed Event, but I dont know how to use it. I try set `mnuOpen =false` in this Event but fail :( – Lê Trung May 11 '17 at 11:29
  • @LêTrung why do you expect `mnuOpen` to be set to false if you click "not in button area" ? – Mong Zhu May 11 '17 at 12:04
  • ahh you want the menu to hide automatically after it has been closed ? so that the user always has to press the button to see it ?=! – Mong Zhu May 11 '17 at 12:05
  • Is it WPF or winforms? Winforms doesn't have the `Closed` event while WPF doesn't have the `Show` and `Hide`... – Ofir Winegarten May 11 '17 at 12:18
  • @OfirWinegarten actually in WinForms `ContextMenuStrip` has a `Closed` event – Mong Zhu May 11 '17 at 13:19
  • the plan with the closing event sound good, but it will fail. You won't be able to close the menu with a second click on the button. Because as soon as you press the mouse click and the focus is not on the menu it will close and set the `mnuOpen` variable to `false`. then the button click event is fired and the menu will show up again. – Mong Zhu May 11 '17 at 14:50

0 Answers0