Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

ToolStripControllerLabel that extends and shrinks ToolStrip control

0.00/5 (No votes)
2 Sep 2006 1  
ToolStripControllerLabel placeable on VisualStudio

ToolStripControlerLabelImg05.PNG

Introduction

ToolStrip control can host menus, items and user controls. The ToolStripItems can be placed and layouted on Visual Studio 2005. The ToolStrip items can be extended and customized by users and that are also placeable on Visual Studio.This article demonstrates the ToolStripControllerLabel that can collapse, extend and close ToolStrip control.

History

2006 Aug. 12, Updated

  • Previous toolStripItems, shrinker and closer were unified in a ToolStripControllerLabel
  • Icon images of the ToolStripControllerLabel were added.

(Author thanks Vainola Harri.)

2006 Sep. 3, Updated

  • Icons were modified.

How does it work ?

Collapsing.PNG

The demo application is a tiny application for demonstrating the ToolStripControllerLabel. (Demo application requires .NET Framework 2.0)

At first, click the Collapse icon icon of 'Header' label. You can see the tool strip shrinks and the icon changes to Expand button icon. Second, click the Expand button icon. Then, the tool strip should extend. This action is performed by the clicked ToolStripControllerLabel. Notice that the actions of 'Header' and 'Footer' are different. The header label changes the visibility of the items that are after it. The footer label changes visibility of the items that are before it.

Another functionality of this label is to 'Close' the tool strip control. Click the Close button icon. The tool strip should close. Actually, the instance of the tool strip is alive, so click the 'Show tool bars' button.

These all functionalities can be performed by a single class without coding on VisualStudio.

Use in ease

ControllerOnVS2005.PNG

This class can be placed on your Windows form by using VS2005. You can use the item like other ToolStripItems by adding ToolStripControllerLabel project or adding reference to ToolStripControllerLabel.dll without any coding.

Previous version of ToolStripControllerLabel class displayed the icons by drawing rectangles and lines in the code. In such way, flexibility of the class was quite limited. One of readers (thanks,) suggested that the instance of icon images might improve this problem. So, I made three public properties of the icons. The images can be modified in VS2005.

Furthermore, the functionalities of 'collapse/expand items' and 'close items' were unified from the previous two classes. The functionality also can be easily chosen on VS2005.

Customizing properties in VS2005

Code in demo application

Since the ToolStripControllerLabel item can be placed on ToolStrip using Visual Studio, it really simplify the codes on Forms or Controls. Actually, demo application has no codes for the actions of collapsing, extending and closing. The code of Form1 has only 5 lines in order to show the ToolBars after it is hidden.

namespace CodeProject
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            toolStrip1.Visible = true;
            toolStrip2.Visible = true;
        }
    }
}

Code in ToolStripControllerLabel

The ToolStripControllerLabel is a subclass of ToolStripLabel. It overrides an OnClick event. OnClick call the following 'DoAction()' method.The following is a part of the DoAction() that changes visibility of the items.

// Collapse or extend

ToolStrip toolStrip = this.Owner;
int idx = toolStrip.Items.IndexOf(this);

toolStrip.SuspendLayout();
this.SetControllerImage();
if (ControllerType == StripControllerTypes.Header)
{
    // --- Header logic ---
    // This loop continues under the following conditions.
    // 1. There is a next item.
    // 2. The next item is not ToolStripControllerLabel
    //
    while (idx + 1 < toolStrip.Items.Count &&
        toolStrip.Items[idx + 1].GetType() != typeof(ToolStripControllerLabel))
    {
        idx++;
        toolStrip.Items[idx].Visible = !isCollapsed;
    }
}
else // if (ControllerType == StripControllerTypes.Footer) 
{
    // --- Footer logic ---
    // This loop continues under the following conditions.
    // 1. There is a previous item.
    // 2. The previous item is not ToolStripControllerLabel
    //
    while (idx - 1 > 0 &&
        toolStrip.Items[idx - 1].GetType() != typeof(ToolStripControllerLabel))
    {
        idx--;
        toolStrip.Items[idx].Visible = !isCollapsed;
    }
}

toolStrip.ResumeLayout();

Conclusion

New ToolStrip functionality that collapses, expands or closes ToolStrip items can be implemented easily using Visual Studio.

Appendix

If you create customized ToolStrip items, add attributes on the custom class. I obtained this information from a blog 'jfo's coding'by searching MSDN forum. It was really helpful. Sometimes customized items don't appear in the menu when using Visual Studio. In such case, re-build or re-open the Form contains the item.

[System.ComponentModel.DesignerCategory("code")]
[ToolStripItemDesignerAvailability
    (ToolStripItemDesignerAvailability.ToolStrip |
    ToolStripItemDesignerAvailability.StatusStrip)]
public class ToolStripShrinker:ToolStripLabel
    {
        public ToolStripShrinker() : base() { }
        ----

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here