I have like 10 items in a ContextMenuStrip. I have an Opening Event handler which decides which of the 10 items will be visible. Based on application logic I may hide many of them by setting Visible = false and Enabled = false;
What happens is in the case that 6/10 are displayed. Their will be an area 4 menu items height of blank space the first time I click the strip. If I right click to make it appear the second time it shows up where it should be. So, the strip basically has the position calculated for a 10 item strip when in reality it's only a 6 item strip.

How do I force it to recalculate?
Here are some of the things I've tried:
contextMenuStrip1.Refresh();
contextMenuStrip1.Update();
contextMenuStrip1.PerformLayout();
contextMenuStrip1.AutoSize = true;
ToolStripItem tempItem = contextMenuStrip1.Items.Add("temp");
contextMenuStrip1.Items.Remove(tempItem);
contextMenuStrip1.Refresh();
contextMenuStrip1.Update();
contextMenuStrip1.Invalidate();
I've found that
AutoSize = false;
AutoSize = true;
after all item manipulations works. Haven't found any other solution.
In my case, I add items on opening (dynamic list of windows), and sometimes item caption was longer than the menu.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With