Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ContextMenuStrip how do you force update (size is wrong b/c I hide some of it's items)

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.

First Click It looks like this 2nd click it looks fine

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();
like image 397
blak3r Avatar asked Oct 29 '25 16:10

blak3r


1 Answers

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.

like image 52
Gman Avatar answered Nov 01 '25 05:11

Gman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!