Full sample - manually handling Overflow
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication5 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
// -----------------------------
// How to use this sample:
// Set all of your items to Overflow.Never
// Sync the toolStrip1.LayoutCompleted event and use toolStrip1_LayoutCompleted handler below
// -----------------------------
// returns the width plus exterior spacing
private static int GetItemWidth(ToolStripItem item) {
return item.Width + item.Margin.Horizontal;
}
// returns the width minus the overflow button and grip.
private static int GetToolStripInteriorWidth(ToolStrip toolStrip) {
int overflowButtonWidth =(toolStrip.OverflowButton.Visible) ? GetItemWidth(toolStrip.OverflowButton) : 0;
return toolStrip.DisplayRectangle.Width - overflowButtonWidth;
}
private void toolStrip1_LayoutCompleted(object sender, EventArgs e) {
toolStrip1.SuspendLayout();
int currentWidthOfAllTheItems = 0;
bool hasOverflowItems = false;
int spaceToFree = 0;
// walk through the collection to see where all the items are
for (int i = 0; i < toolStrip1.Items.Count; i++ ) {
ToolStripItem item = toolStrip1.Items[i];
if (item.Available) {
if (item.Placement == ToolStripItemPlacement.Main) {
currentWidthOfAllTheItems += GetItemWidth(item);
}
else if (item.Placement == ToolStripItemPlacement.None) {
// we currently cant display an item we'd like to display on Main
// sum up how much space we need to free. We'll then send items to
// the overflow in order to get this item back.
spaceToFree = GetItemWidth(item);
}
else {
// if we have overflow items and extra space, we should try to bring
// back the item.
hasOverflowItems = true;
}
}
}
// subtract the currently empty space on the toolstrip
spaceToFree -= GetToolStripInteriorWidth(toolStrip1) - currentWidthOfAllTheItems;
// walk from the front of the collection to the end
// sending items to the overflow.
if (spaceToFree > 0) {
for (int i = 0; spaceToFree > 0 && i < toolStrip1.Items.Count; i++) {
ToolStripItem item = toolStrip1.Items[i];
if (item.Available && item.Placement == ToolStripItemPlacement.Main) {
spaceToFree -= GetItemWidth(item);
item.Overflow = ToolStripItemOverflow.Always;
}
}
}
else if (hasOverflowItems) {
// calculate how much space we've got left on the bar
int remainingWidth = GetToolStripInteriorWidth(toolStrip1) - currentWidthOfAllTheItems;
// walk backwards through the items so we restore the last
// item we sent there
for (int i = toolStrip1.Items.Count - 1; i >= 0; i--) {
ToolStripItem item = toolStrip1.Items[i];
if (item.Placement == ToolStripItemPlacement.Overflow) {
int requiredWidthToDisplayNextItem = GetItemWidth(item);
if (requiredWidthToDisplayNextItem <= remainingWidth) {
remainingWidth -= requiredWidthToDisplayNextItem;
item.Overflow = ToolStripItemOverflow.Never;
}
else {
// if we cannot fit an item we immediately break as we bring
// them back in collection order.
break;
}
}
}
}
toolStrip1.ResumeLayout();
}
}
}