Microsoft Communities

Welcome to WindowsClient.net | Sign in | Join

Here are some frequently asked questions about Windows Forms and their answers.

Windows Forms FAQs

Do ToolStrips automatically eliminate duplicate or trailing separators from layout?

No, but the following code will do it for you:

void DropDown_Layout(object sender, LayoutEventArgs e)
{
    StripSeparators(((ToolStrip)sender).Items);
}

public void StripSeparators(ToolStripItemCollection tsItems)
{
// ItemCollections should neither begin with, end with or contain adjacent separators.
    bool itemDisplayed = false;
    ToolStripItem lastSeparator = null;

    foreach (ToolStripItem tsItem in tsItems)
    {
      if (tsItem.Available)
      {
        if (tsItem is ToolStripSeparator)
        {
           // hide all separators
           tsItem.Visible = false;
           // only set in a separator that has a chance to be shown later
           // if no items have been displayed, no chance
           if (itemDisplayed) lastSeparator = tsItem;
        }
        else if (lastSeparator != null)
        {
           // show it when valid
           lastSeparator.Visible = true;
        }
        else
        {
           itemDisplayed = true;
        }
      }
    }
}


Page view counter