A ContextMenuStrip on a button does not behave as expected

Ihandler 21 Reputation points
2021-04-13T11:01:03.313+00:00

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
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,811 questions
{count} votes

Accepted answer
  1. Daniel Zhang-MSFT 9,611 Reputation points
    2021-04-14T02:29:57.753+00:00

    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.

    0 comments No comments

0 additional answers

Sort by: Most helpful