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
How does it work ?
The demo application is a tiny application for demonstrating the ToolStripControllerLabel. (Demo application requires .NET Framework 2.0)
At first, click the icon of 'Header' label. You can see the tool strip shrinks and the icon changes to icon. Second, click the 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 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
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.
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.
ToolStrip toolStrip = this.Owner;
int idx = toolStrip.Items.IndexOf(this);
toolStrip.SuspendLayout();
this.SetControllerImage();
if (ControllerType == StripControllerTypes.Header)
{
while (idx + 1 < toolStrip.Items.Count &&
toolStrip.Items[idx + 1].GetType() != typeof(ToolStripControllerLabel))
{
idx++;
toolStrip.Items[idx].Visible = !isCollapsed;
}
}
else
{
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() { }
----