Find ContextMenuStrip height before showing

I have a ContextMenuStrip (ctMenuMassEdit) that I want to display when I left-click (btnMassEdit). I want the ContextMenuStrip to appear above the button, i.e. the position (0, -ContextMenuStrip.Height) relative to the button:

private void btnMassEdit_Click(object sender, EventArgs e) { ctMenuMassEdit.Show(btnMassEdit, new Point(0, -ctMenuMassEdit.Height)); } 

However, the Height property is 0 the first time the button is clicked (I assume that ContextMenuStrip is not created before it is shown for the first time), and the result is that ContextMenuStrip appears on top of the button. The second time I press the button, however it is displayed in the correct position, so the main logic of my logic is at least correct.

I tried to add the following before showing ContextMenuStrip, but this did not work as I would like:

 if (!ctMenuMassEdit.Created) { ctMenuMassEdit.CreateControl(); } 

So, is there a way to create a ContextMenuStrip before showing it for the first time, so that I have the correct Height property? I could, of course, crack it, hide it and show it again, but that doesn't seem really neat ...

+4
source share
2 answers

how about ctMenuMassEdit.Show (btnMassEdit, Me.PointToScreen (btnMassEdit.Location), ToolStripDropDownDirection.AboveRight);

+2
source

Since someone had no suggestions, I can just share what ended up being my decision. This is not a solution, but a hack, but if I hide it and show it again for the first time, it works:

 private void btnMassEdit_Click(object sender, EventArgs e) { if (ctMenuMassEdit.Height < 5) { ctMenuMassEdit.Show(btnMassEdit, new Point(0, -ctMenuMassEdit.Height)); ctMenuMassEdit.Hide(); } ctMenuMassEdit.Show(btnMassEdit, new Point(0, -ctMenuMassEdit.Height)); } 

You may wonder why I check Height <5, and not just Height == 0? Well, for some strange reason, ContextMenuStrip seemed to have a height of 4 before I displayed it for the first time (and not 0, as you might suggest), so this is another hack inside the hack;)

0
source

Source: https://habr.com/ru/post/1308115/


All Articles