11 KiB
title, ms.date, dev_langs, helpviewer_keywords, ms.assetid
| title | ms.date | dev_langs | helpviewer_keywords | ms.assetid | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Walkthrough: Creating a Professionally Styled ToolStrip Control | 03/30/2017 |
|
|
b52339ae-f1d3-494e-996e-eb455614098a |
Walkthrough: Creating a Professionally Styled ToolStrip Control
You can give your application’s xref:System.Windows.Forms.ToolStrip controls a professional appearance and behavior by writing your own class derived from the xref:System.Windows.Forms.ToolStripProfessionalRenderer type.
This walkthrough demonstrates how to use xref:System.Windows.Forms.ToolStrip controls to create a composite control that resembles the Navigation Pane provided by Microsoft® Outlook®. The following tasks are illustrated in this walkthrough:
-
Creating a Windows Control Library project.
-
Designing the StackView Control.
-
Implementing a Custom Renderer.
When you are finished, you will have a reusable custom client control with the professional appearance of a Microsoft Office® XP control.
To copy the code in this topic as a single listing, see How to: Create a Professionally Styled ToolStrip Control.
Prerequisites
You'll need Visual Studio to complete this walkthrough.
Create the control library project
-
In Visual Studio, create a new Windows Control Library project named
StackViewLibrary. -
In Solution Explorer, delete the project's default control by deleting the source file named "UserControl1.cs" or "UserControl1.vb", depending on your language of choice.
For more information, see How to: Remove, Delete, and Exclude Items.
-
Add a new xref:System.Windows.Forms.UserControl item to the StackViewLibrary project. Give the new source file a base name of
StackView.
Design the StackView control
The StackView control is a composite control with one child xref:System.Windows.Forms.ToolStrip control. For more information about composite controls, see Varieties of Custom Controls.
-
From the Toolbox, drag a xref:System.Windows.Forms.ToolStrip control to the design surface.
-
In the Properties window, set the xref:System.Windows.Forms.ToolStrip control's properties according to the following table.
Property Value Name stackStripCanOverflow falseDock xref:System.Windows.Forms.DockStyle.Bottom Font Tahoma, 10pt, style=BoldGripStyle xref:System.Windows.Forms.ToolStripGripStyle.Hidden LayoutStyle xref:System.Windows.Forms.ToolStripLayoutStyle.VerticalStackWithOverflow Padding 0, 7, 0, 0RenderMode xref:System.Windows.Forms.ToolStripRenderMode.Professional -
In the Windows Forms Designer, click the xref:System.Windows.Forms.ToolStrip control's Add button and add a xref:System.Windows.Forms.ToolStripButton to the
stackStripcontrol. -
In the Properties window, set the xref:System.Windows.Forms.ToolStripButton control's properties according to the following table.
Property Value Name mailStackButtonCheckOnClick true CheckState xref:System.Windows.Forms.CheckState.Checked DisplayStyle xref:System.Windows.Forms.ToolStripItemDisplayStyle.ImageAndText ImageAlign xref:System.Drawing.ContentAlignment.MiddleLeft ImageScaling xref:System.Windows.Forms.ToolStripItemImageScaling.None ImageTransparentColor 238, 238, 238Margin 0, 0, 0, 0Padding 3, 3, 3, 3Text Mail TextAlign xref:System.Drawing.ContentAlignment.MiddleLeft -
Repeat step 7 for three more xref:System.Windows.Forms.ToolStripButton controls.
Name the controls
calendarStackButton,contactsStackButton, andtasksStackButton. Set the value of the xref:System.Windows.Forms.Control.Text%2A property to Calendar, Contacts, and Tasks, respectively.
Handle events
Two events are important to make the StackView control behave correctly. Handle the xref:System.Windows.Forms.UserControl.Load event to position the control correctly. Handle the xref:System.Windows.Forms.ToolStripItem.Click event for each xref:System.Windows.Forms.ToolStripButton to give the StackView control state behavior similar to the xref:System.Windows.Forms.RadioButton control.
-
In the Windows Forms Designer, select the
StackViewcontrol. -
In the Properties window, click Events.
-
Double-click the Load event to generate the
StackView_Loadevent handler. -
In the
StackView_Loadevent handler, copy and paste the following code.[!code-csharpSystem.Windows.Forms.ToolStrip.StackView#3] [!code-vbSystem.Windows.Forms.ToolStrip.StackView#3]
-
In the Windows Forms Designer, select the
mailStackButtoncontrol. -
In the Properties window, click Events.
-
Double-click the Click event.
The Windows Forms Designer generates the
mailStackButton_Clickevent handler. -
Rename the
mailStackButton_Clickevent handler tostackButton_Click.For more information, see Rename a code symbol refactoring.
-
Insert the following code into the
stackButton_Clickevent handler.[!code-csharpSystem.Windows.Forms.ToolStrip.StackView#4] [!code-vbSystem.Windows.Forms.ToolStrip.StackView#4]
-
In the Windows Forms Designer, select the
calendarStackButtoncontrol. -
In the Properties window, set the Click event to the
stackButton_Clickevent handler. -
Repeat steps 10 and 11 for the
contactsStackButtonandtasksStackButtoncontrols.
Define icons
Each StackView button has an associated icon. For convenience, each icon is represented as a Base64-encoded string, which is deserialized before a xref:System.Drawing.Bitmap is created from it. In a production environment, you store bitmap data as a resource, and your icons appear in the Windows Forms Designer. For more information, see How to: Add Background Images to Windows Forms.
-
In the Code Editor, insert the following code into the
StackViewclass definition. This code initializes the bitmaps for the xref:System.Windows.Forms.ToolStripButton icons.[!code-csharpSystem.Windows.Forms.ToolStrip.StackView#2] [!code-vbSystem.Windows.Forms.ToolStrip.StackView#2]
-
Add a call to the
InitializeImagesmethod in theStackViewclass constructor.[!code-csharpSystem.Windows.Forms.ToolStrip.StackView#5] [!code-vbSystem.Windows.Forms.ToolStrip.StackView#5]
Implement a custom renderer
You can customize most elements of the StackView control my implementing a class that derives from the xref:System.Windows.Forms.ToolStripRenderer class. In this procedure, you will implement a xref:System.Windows.Forms.ToolStripProfessionalRenderer class that customizes the grip and draws gradient backgrounds for the xref:System.Windows.Forms.ToolStripButton controls.
-
Insert the following code into the
StackViewcontrol definition.This is the definition for the
StackRendererclass, which overrides the xref:System.Windows.Forms.ToolStripRenderer.RenderGrip, xref:System.Windows.Forms.ToolStripRenderer.RenderToolStripBorder, and xref:System.Windows.Forms.ToolStripRenderer.RenderButtonBackground methods to produce a custom appearance.[!code-csharpSystem.Windows.Forms.ToolStrip.StackView#10] [!code-vbSystem.Windows.Forms.ToolStrip.StackView#10]
-
In the
StackViewcontrol's constructor, create a new instance of theStackRendererclass and assign this instance to thestackStripcontrol's xref:System.Windows.Forms.ToolStrip.Renderer%2A property.[!code-csharpSystem.Windows.Forms.ToolStrip.StackView#5] [!code-vbSystem.Windows.Forms.ToolStrip.StackView#5]
Test the StackView control
The StackView control derives from the xref:System.Windows.Forms.UserControl class. Therefore, you can test the control with the UserControl Test Container. For more information, see How to: Test the Run-Time Behavior of a UserControl.
-
Press F5 to build the project and start the UserControl Test Container.
-
Move the pointer over the buttons of the
StackViewcontrol, and then click a button to see the appearance of its selected state.
Next steps
In this walkthrough, you have created a reusable custom client control with the professional appearance of an Office XP control. You can use the xref:System.Windows.Forms.ToolStrip family of controls for many other purposes:
-
Create shortcut menus for your controls with xref:System.Windows.Forms.ContextMenuStrip. For more information, see ContextMenu Component Overview.
-
Create a form with an automatically populated standard menu. For more information, see Walkthrough: Providing Standard Menu Items to a Form.
-
Create a multiple document interface (MDI) form with docking xref:System.Windows.Forms.ToolStrip controls. For more information, see How to: Create an MDI Form with Menu Merging and ToolStrip Controls.