Click here to Skip to main content
Click here to Skip to main content

ToolStripControllerLabel that extends and shrinks ToolStrip control

, 2 Sep 2006
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Iquoba
Web Developer
Japan Japan
Iquoba is a developer of software for bioscience and biotechnology at Symplus corp., who started with BASIC or Z80 assembler when he was twelve.

Comments and Discussions

 
GeneralQuery about new Option Pinmembermananthan756-Jun-07 0:18 
GeneralRe: Query about new Option PinmemberIquoba6-Jun-07 4:09 
GeneralExcellent Enhancement to the Toolstrip PinmemberJhoward18-Aug-06 6:23 
GeneralRe: Excellent Enhancement to the Toolstrip PinmemberIquoba18-Aug-06 16:19 
GeneralTypo PinmemberThe_Mega_ZZTer13-Aug-06 9:38 
GeneralRe: Typo PinmemberIquoba13-Aug-06 13:45 
NewsNew features PinmemberIquoba12-Aug-06 17:28 
GeneralToolStripHeader - Collapsible toolstrip PinmemberVäinölä Harri1-Aug-06 19:54 
AnswerRe: ToolStripHeader - Collapsible toolstrip PinmemberIquoba2-Aug-06 6:07 
GeneralRe: ToolStripHeader - Collapsible toolstrip PinmemberIquoba12-Aug-06 20:10 
GeneralRe: ToolStripHeader - Collapsible toolstrip PinmemberVäinölä Harri2-Sep-06 9:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140902.1 | Last Updated 2 Sep 2006
Article Copyright 2006 by Iquoba
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid