question

Ihandler avatar image
0 Votes"
Ihandler asked Ihandler commented

A ContextMenuStrip on a button does not behave as expected

private void btnButton_Click(object sender, EventArgs e)
{
ContextMenuStrip buttonMenu = new ContextMenuStrip();

buttonMenu.Items.Clear();

buttonMenu.Items.Add("Item A");
buttonMenu.Items.Add("Item B");
buttonMenu.Items.Add("Item C");

buttonMenu.Show(btnButton, new Point(0, btnButton.Height));
}

The coding above works fine if you place a button (click event) on a WinForm. Just click on a button and a menu is displayed.

However, if you place a button on a "user control" and click on it. The menu will be displayed with TWO clicks. Yes, the first click doesn't show up the menu, the second and third and so on.... works normally.

I do not understand the reason behind. If anyone knows how to fix this issue?

windows-forms
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

sorry about that, I made a careless mistake as I added a line of code (hide contextmenustrip) in the base form and therefore something wrong was happened.

And I have removed the code and it works normally.

Thanks again !

0 Votes 0 ·

1 Answer

DanielZhang-MSFT avatar image
0 Votes"
DanielZhang-MSFT answered DanielZhang-MSFT edited

Hi
I add a UserControl(UserControl1) with a button(button1)into my form1.
And put your code in the button1_Click event of the UserControl1, I click the button once and the menu is displayed.
Here is a code example:

 public partial class UserControl1 : UserControl
 {
     public UserControl1()
     {
         InitializeComponent();
     }
    
     private void button1_Click(object sender, EventArgs e)
     {
         ContextMenuStrip buttonMenu = new ContextMenuStrip();
    
         buttonMenu.Items.Clear();
    
         buttonMenu.Items.Add("Item A");
         buttonMenu.Items.Add("Item B");
         buttonMenu.Items.Add("Item C");
    
         buttonMenu.Show(button1, new Point(0, button1.Height));
     }
 }

If I misunderstand, please explain in detail or provide some code to reproduce the situation.
Best Regards,
Daniel Zhang


If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.