Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my MenuStrip appear in the incorrect Location on first click?

I'm working on a VB Winforms project (although I'm just as fine with a C# solution) and have the following set up:

  • I have a ContextMenuStrip on the form, msCreateReports
  • I have a MenuStrip at the top of the form with one menu item being Create Reports and its DropDown set to msCreateReports
  • I have a command button on the form cmdCreateReports

Now, for my command button, I have the following code for its click event:

Private Sub cmdCreateReports_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCreateReports.Click
    msCreateReports.Show(Cursor.Position.X, Cursor.Position.Y)
End Sub

EDIT (Update / Explanation):


Basically, what I'm looking for functionality-wise is to have this contextmenustrip be able to show up in one of 2 different places, depending on where the user clicks... If they click on the menu option, the context menu appears as a drop-down on the Create Reports menu item OR if the user clicks the command button, the menu will apeear as a context menu on the side of the mouse pointer. I want the same menu to be able to appear on either one of these two locations depending on where the user clicks to make this menu appear.


Now, my problem is that only the first time the command button is clicked, the ContextMenuStrip appears up top by the menu, rather than on top of the command button, as I would like it to.

After the first click, the menu appears in the correct location... What did I do wrong / how can I fix this??

Thanks!!

like image 728
John Bustos Avatar asked Feb 16 '23 21:02

John Bustos


1 Answers

I have the same problem (I'm using Visual Studio 2010 SP1, and C#). I don't think we did anything wrong, it looks like a Winforms bug to me.

I fixed it like this:

1) I have unset (using the visual designer) the DropDown property of the main strip item.

2) I have defined the Opening event on the contextMenuStrip, and the DropDownOpening event on the main strip item like this:

private void toolStripMyMenuItem_DropDownOpening(object sender, EventArgs e)
{
    toolStripMyMenuItemMyLists.DropDown = contextMenuStrip;
}

private void contextMenuStrip_Opening(object sender, CancelEventArgs e)
{
    toolStripMyMenuItemMyLists.DropDown = contextMenuStrip;
}

And I don't have this problem anymore. Hope it will help you too :-)

like image 158
Simon Mourier Avatar answered Feb 20 '23 04:02

Simon Mourier