dotnetmonitor.com |
|
||||||||||||
| To be precise, overflow is a feature of the ToolStrips
default LayoutStyle (StackWithOverflow), not of the ToolStrip control itself. You
determine what overflow action is preferred via the Overflow property (Always, Never,
AsNeeded). To determine dynamically where each item was laid out, sync the LayoutCompleted
event check item Placement (Main, Overflow, None). Property changes here will cause
another layout; be wary of recursive code here. An item will not be partially laid
out if it would result in a truncation it will not be laid out and Placement will
be none.
NOTE: There is an SDK sample on dynamic DisplayStyle as an alternative to Overflow. Dynamic Overflow sampleThe following demonstrates basic overflow. Run the form and resize to the see the output after layout.
ToolStrip t = new ToolStrip(); t.LayoutCompleted += new EventHandler(t_LayoutCompleted); ToolStripButton tsb1 = new ToolStripButton("1 - Left - Never "); tsb1.Overflow = ToolStripItemOverflow.Never;
ToolStripButton tsb2 = new ToolStripButton("2 - Right- AsNeeded"); tsb2.Alignment = ToolStripItemAlignment.Right;
ToolStripButton tsb3 = new ToolStripButton("3 - Left - AsNeeded");
ToolStripButton tsb4 = new ToolStripButton("4 - Right- AsNeeded"); tsb4.Alignment = ToolStripItemAlignment.Right;
ToolStripButton tsb5 = new ToolStripButton("5 - Left - Always "); tsb5.Overflow = ToolStripItemOverflow.Always;
t.Items.AddRange(new ToolStripItem[] {tsb1, tsb2, tsb3, tsb4, tsb5});
this.Controls.Add(t);
void t_LayoutCompleted(object sender, EventArgs e) { foreach (ToolStripItem item in ((ToolStrip)(sender)).Items) { System.Diagnostics.Debug.WriteLine(item.ToString() + " Placement: " + item.Placement.ToString()); } } Custom DropDownThe default Overflow dropdown is available programatically but to customize the layout of the overflow, you can also simply assign your custom ToolStripDropDownMenu to the dropdown property. Below is an example of a custom dropdown and handling overflow items manually.
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms;
namespace WindowsApplication65 { public partial class Form1 : Form {
ContextMenuStrip cms = new ContextMenuStrip(); ToolStrip toolstrip = new ToolStrip(); ToolStripButton placeholder = new ToolStripButton();
ToolStripItem[] originalItemCollection;
public Form1() { InitializeComponent();
toolstrip.Layout += new LayoutEventHandler(toolstrip_Layout); toolstrip.LayoutCompleted += new EventHandler(toolstrip_LayoutCompleted);
// the is a way to fake the overflow has stuff in it, therefore the button will // get rendered. // placeholder.Overflow = ToolStripItemOverflow.Always; toolstrip.Items.Add(placeholder);
toolstrip.SuspendLayout(); // add some items. toolstrip.Items.Add("1"); toolstrip.Items.Add("2"); toolstrip.Items.Add("3"); toolstrip.Items.Add("4"); toolstrip.Items.Add("5"); toolstrip.Items.Add("6"); toolstrip.Items.Add("7"); toolstrip.Items.Add("8");
// we need to snap the items collection before performing a layout // as the LayoutCompleted will remove items from the items collection. originalItemCollection = new ToolStripItem[toolstrip.Items.Count]; toolstrip.Items.CopyTo(originalItemCollection, 0);
toolstrip.ResumeLayout();
this.Controls.Add(toolstrip); toolstrip.OverflowButton.DropDown = cms; }
void toolstrip_Layout(object sender, LayoutEventArgs e) { // push all items back into main toolstrip collection if (originalItemCollection != null) { toolstrip.Items.AddRange(originalItemCollection); } }
void toolstrip_LayoutCompleted(object sender, EventArgs e) { ToolStripItemCollection items = toolstrip.Items;
toolstrip.SuspendLayout(); cms.SuspendLayout(); // turn overflow button off placeholder.Available = false; for (int i = items.Count - 1; i > -1; i--) { ToolStripItem item = items[i]; System.Diagnostics.Debug.WriteLine(item.ToString() + " Placement: " + item.Placement.ToString()); if (item.Placement == ToolStripItemPlacement.Overflow) { if (item != placeholder) { cms.Items.Insert(0, item);
// turn overflow button on placeholder.Available = true; } } } cms.ResumeLayout(false); toolstrip.ResumeLayout(true);
} } } |
||||||||||||
|
||||||||||||