Dokuwiki for science

差分

この文書の現在のバージョンと選択したバージョンの差分を表示します。

この比較画面にリンクする

toolstripshrinker [2013/01/27 03:15] (現在)
ライン 1: ライン 1:
 +
 +This article is also published at **CodeProject**\\ -> http://​www.codeproject.com/​useritems/​ToolStripShrinker.asp
 +
 +<float right>
 +<select Language>​
 +computer:​toolstripshrinker|日本語
 +computer:​en:​toolstripshrinker|English
 +</​select>​
 +</​float>​
 +=====ToolStripShrinker=====
 +<​html>​
 +<UL class=download>​
 +<​LI><​A href="​http://​www.codeproject.com/​useritems/​ToolStripShrinker/​ToolStripControllerLabel_demo.zip">​Download demo application - 11.1 Kb</​A> ​
 +<​LI><​A href="​http://​www.codeproject.com/​useritems/​ToolStripShrinker/​ToolStripControllerLabel_src.zip">​Download source code - 28.5 Kb</​A>​ </​LI></​UL>​
 +</​html>​
 +<​clear/>​
 +<float right200>​
 +^Environment||
 +^IDE|VS2005|
 +^Lang|C#|
 +^Lib|.NET Framework 2.0|
 +</​float>​
 +{{http://​www.codeproject.com/​useritems/​ToolStripShrinker/​ToolStripControlerLabelImg05.PNG}}
 +
 +====Introduction====
 +<​html>​
 +<​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>​
 +
 +</​html>​
 +
 +====How does it work ?====
 +<​html>​
 +<​P><​IMG alt="​Collapsing"​ src="​http://​www.codeproject.com/​useritems/​ToolStripShrinker/​Collapsing.PNG"></​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="​http://​www.codeproject.com/​useritems/​ToolStripShrinker/​minus.png"​ 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="​http://​www.codeproject.com/​useritems/​ToolStripShrinker/​plus.png"​ width=11>​ icon. Second, click the <IMG height=11 alt="​Expand button"​ src="​http://​www.codeproject.com/​useritems/​ToolStripShrinker/​minus.png"​ 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="​http://​www.codeproject.com/​useritems/​ToolStripShrinker/​close.png"​ 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>
 +</​html>​
 +===Use in ease===
 +<​html>​
 +<IMG style="​FLOAT:​ right; margin-left:​20px;"​ alt="​Placeable in VS2005"​ src="​http://​www.codeproject.com/​useritems/​ToolStripShrinker/​ControllerOnVS2005.PNG"> ​
 +<​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="​http://​www.codeproject.com/​useritems/​ToolStripShrinker/​tsItemPropertiesPNG.PNG"​ width=305> ​
 +</​html>​
 +
 +
 +====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. ​
 +<code csharp>
 +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>​
 +
 +
 +====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. ​
 +
 +<code csharp>
 +// 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 &lt; toolStrip.Items.Count &​amp;&​amp;​
 +        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 &gt; 0 &​amp;&​amp;​
 +        toolStrip.Items[idx - 1].GetType() != typeof(ToolStripControllerLabel))
 +    {
 +        idx--;
 +        toolStrip.Items[idx].Visible = !isCollapsed;​
 +    }
 +}
 +
 +toolStrip.ResumeLayout();​
 +</​code>​
 +
 +====Conclusion====
 +<​html>​
 +<​P>​New <​CODE>​ToolStrip</​CODE>​ functionality that collapses, expands or closes ToolStrip items can be implemented easily using Visual Studio. </P>
 +</​html>​
 +
 +
 +====Appendix====
 +<​html>​
 +If you create customized <​CODE>​ToolStrip</​CODE>​ items, add attributes on the custom class. I obtained this information from a blog <A href="​http://​blogs.msdn.com/​jfoscoding/​articles/​477428.aspx">'​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.
 +</​html>​
 +<code csharp>
 +[System.ComponentModel.DesignerCategory("​code"​)]
 +[ToolStripItemDesignerAvailability
 +    (ToolStripItemDesignerAvailability.ToolStrip |
 +    ToolStripItemDesignerAvailability.StatusStrip)]
 +public class ToolStripShrinker:​ToolStripLabel
 +    {
 +        public ToolStripShrinker() : base() { }
 +        ----
 +</​code>​
 +=====Links=====
 +<float left>​{{icon>​compass_large}}</​float>​
 +<float left>
 +  *[[ToolStripShrinker]]
 +  *[[ExtendedPictureBox]]
 +</​float>​
 +<​clear/>​