I have a program with a MenuStrip at the top, it currently has 4 items. What I’m essentially trying to do is make sure that all 4 of these menu items are visible if the user shrinks the width.
My initial attempt was to get the total number of items on the MenuStrip by simply grabbing the item collection then grab the bounds of the last item on the strip. After retrieving the bounds variable, I then do a quick calculation of the bounds.x + bounds.width to make sure that the form.width is not less than that, if so, it resizes. All of this takes place within the ResizeEnd event for the form.
private void F_Main_ResizeEnd(object sender, EventArgs e)
{
ToolStripItemCollection t_col = menuStrip1.Items;
int _howMany = t_col.Count;
var mi_bounds = t_col[_howMany - 1].Bounds;
if (this.Width < (mi_bounds.X + mi_bounds.Width))
this.Width = (mi_bounds.X + mi_bounds.Width);
}
Now, the code seems to work, most of the time. I found that if I shrunk the form so that either the 2nd or 3rd item dissapeared, it resized too thin and left the 4th menu invisible. At that point, interestingly enough, if I simply clicked on the right edge of the form – as if I would be resizing but not moving the form, it would then recover to the correct width and display all 4 menu items.
What I found was that the bounds property of the toolstripmenuitem changed and was value {a} when I started resizing with all 4 showing and value {b} when only 3 were showing.
I’m now trying to figure out a more reliable way to make sure that all my menu items are visible and the form resize to the proper width.
Sorry if this doesn’t explain it properly as this is a weird problem for me.
“I’m now trying to figure out a more reliable way to make sure that all my menu items are visible and the form resize to the proper width.”
Using resize events to prevent the form being too small is a clunky way of doing it.
I highly recommend you set the Forms Minimum Width and Height Properties.
Edit:
I’m suggesting these properties be set at run-time after the menu’s have been created &/or after new ones are added execute this (pseudo) code: