Dokuwiki for science

Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /home/symplus3/ on line 1552

This article is also published at CodeProject

<float right>



<UL class=download> <LI><A href="">Download demo application - 11.1 Kb</A> <LI><A href="">Download source code - 28.5 Kb</A> </LI></UL> <clear/> <float right200>

Lib.NET Framework 2.0



<P><CODE>ToolStrip</CODE> control can host menus, items and user controls. The ToolStripItems can be placed and layouted on Visual Studio 2005. The <CODE>ToolStrip</CODE> items can be extended and customized by users and that are also placeable on Visual Studio.This article demonstrates the <CODE>ToolStripControllerLabel</CODE> that can collapse, extend and close <CODE>ToolStrip</CODE> control.</P> <P><STRONG>History</STRONG></P>   <P>2006 Aug. 12, Updated</P> <UL> <LI>Previous toolStripItems, shrinker and closer were unified in a ToolStripControllerLabel <LI>Icon images of the ToolStripControllerLabel were added.</LI></UL> <P>(Author thanks Vainola Harri.)</P> <P>2006 Sep. 3, Updated</P> <UL> <LI>Icons were modified.</LI></UL>

How does it work ?

<P><IMG alt="Collapsing" src=""></P>   <P>The demo application is a tiny application for demonstrating the ToolStripControllerLabel. (Demo application requires .NET Framework 2.0) </P> <P>At first, click the <IMG height=11 alt="Collapse icon" src="" width=11> icon of 'Header' label. You can see the tool strip shrinks and the icon changes to <IMG height=11 alt="Expand button" src="" width=11> icon. Second, click the <IMG height=11 alt="Expand button" src="" width=11> 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.</P> <P>Another functionality of this label is to 'Close' the tool strip control. Click the <IMG height=11 alt="Close button" src="" width=11> icon. The tool strip should close. Actually, the instance of the tool strip is alive, so click the 'Show tool bars' button. </P> <P>These all functionalities can be performed by a single class without coding on VisualStudio. </P>

Use in ease

<IMG style="FLOAT: right; margin-left:20px;" alt="Placeable in VS2005" src=""> <P>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. </P> <P>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.</P> <P>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.</P><IMG style="CLEAR: right" height=93 alt="Customizing properties in VS2005" src="" width=305>

Code in demo application

Since the ToolStripControllerLabel item can be placed on <CODE>ToolStrip</CODE> 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&nbsp;5 lines in order to show the ToolBars after it is hidden.

namespace CodeProject
    public partial class Form1 : Form
        public Form1()
        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);
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 &lt; toolStrip.Items.Count &amp;&amp;
        toolStrip.Items[idx + 1].GetType() != typeof(ToolStripControllerLabel))
        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 &gt; 0 &amp;&amp;
        toolStrip.Items[idx - 1].GetType() != typeof(ToolStripControllerLabel))
        toolStrip.Items[idx].Visible = !isCollapsed;


<P>New <CODE>ToolStrip</CODE> functionality that collapses, expands or closes ToolStrip items can be implemented easily using Visual Studio. </P>


If you create customized <CODE>ToolStrip</CODE> items, add attributes on the custom class. I obtained this information from a blog <A href="">'jfo's coding'</A>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.

    (ToolStripItemDesignerAvailability.ToolStrip |
public class ToolStripShrinker:ToolStripLabel
        public ToolStripShrinker() : base() { }

<float left>search?q=compass_large&amp;btnI=lucky</float> <float left>

</float> <clear/>