Find height of ContextMenuStrip before showing it

Posted by Nailuj on Stack Overflow See other posts from Stack Overflow or by Nailuj
Published on 2010-04-27T07:59:13Z Indexed on 2010/04/27 8:03 UTC
Read the original article Hit count: 424

Filed under:
|
|
|

I have a ContextMenuStrip (ctMenuMassEdit) that I want to display when left-clicking a button (btnMassEdit). I want the ContextMenuStrip to be displayed above the button, i.e. 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 the ContextMenuStrip isn't created before it is shown the first time), and the result is that the ContextMenuStrip appears on top of the button. The 2nd time I click the button however, it appears in the correct position, so the basic of my logic is at least correct.

I tried adding the following before showing the ContextMenuStrip, but it didn't work as hoped:

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

So, is there a way I can create the ContextMenuStrip before showing it the first time, so I have the correct Height property? I could of course use the hack of showing it, hiding it and showing it again, but that doesn't seem really neat...

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET