Permalink
Fetching contributors…
Cannot retrieve contributors at this time
78 lines (60 sloc) 6.5 KB
title ms.custom ms.date ms.prod ms.reviewer ms.suite ms.technology ms.tgt_pltfrm ms.topic dev_langs helpviewer_keywords ms.assetid caps.latest.revision author ms.author manager
How to: Wrap a Windows Forms Control with ToolStripControlHost
03/30/2017
.net-framework
dotnet-winforms
article
jsharp
ToolStripControlHost class
ToolStrip control [Windows Forms], wrapping controls
toolbars [Windows Forms], wrapping controls
ToolStrip control [Windows Forms], hosting controls
e2ce4990-661d-4882-a116-8a9eb575dc84
15
dotnet-bot
dotnetcontent
wpickett

How to: Wrap a Windows Forms Control with ToolStripControlHost

xref:System.Windows.Forms.ToolStripControlHost is designed to enable hosting of arbitrary Windows Forms controls by using the xref:System.Windows.Forms.ToolStripControlHost constructor or by extending xref:System.Windows.Forms.ToolStripControlHost itself. It is easier to wrap the control by extending xref:System.Windows.Forms.ToolStripControlHost and implementing properties and methods that expose frequently used properties and methods of the control. You can also expose events for the control at the xref:System.Windows.Forms.ToolStripControlHost level.

To host a control in a ToolStripControlHost by derivation

  1. Extend xref:System.Windows.Forms.ToolStripControlHost. Implement a default constructor that calls the base class constructor passing in the desired control.

    [!code-cppSystem.Windows.Forms.ToolStripControlHost#10] [!code-csharpSystem.Windows.Forms.ToolStripControlHost#10] [!code-vbSystem.Windows.Forms.ToolStripControlHost#10]

  2. Declare a property of the same type as the wrapped control and return Control as the correct type of control in the property's accessor.

    [!code-cppSystem.Windows.Forms.ToolStripControlHost#11] [!code-csharpSystem.Windows.Forms.ToolStripControlHost#11] [!code-vbSystem.Windows.Forms.ToolStripControlHost#11]

  3. Expose other frequently used properties and methods of the wrapped control with properties and methods in the extended class.

    [!code-cppSystem.Windows.Forms.ToolStripControlHost#12] [!code-csharpSystem.Windows.Forms.ToolStripControlHost#12] [!code-vbSystem.Windows.Forms.ToolStripControlHost#12]

  4. Optionally, override the xref:System.Windows.Forms.ToolStripControlHost.OnSubscribeControlEvents%2A, and xref:System.Windows.Forms.ToolStripControlHost.OnUnsubscribeControlEvents%2A methods and add the control events you want to expose.

    [!code-cppSystem.Windows.Forms.ToolStripControlHost#16] [!code-csharpSystem.Windows.Forms.ToolStripControlHost#16] [!code-vbSystem.Windows.Forms.ToolStripControlHost#16]

  5. Provide the necessary wrapping for the events you want to expose.

    [!code-cppSystem.Windows.Forms.ToolStripControlHost#17] [!code-csharpSystem.Windows.Forms.ToolStripControlHost#17] [!code-vbSystem.Windows.Forms.ToolStripControlHost#17]

Example

[!code-cppSystem.Windows.Forms.ToolStripControlHost#13] [!code-csharpSystem.Windows.Forms.ToolStripControlHost#13] [!code-vbSystem.Windows.Forms.ToolStripControlHost#13]

Compiling the Code

  • This example requires:

  • References to the System and System.Windows.Forms assemblies.

For information about building this example from the command line for [!INCLUDEvbprvb] or [!INCLUDEcsprcs], see Building from the Command Line or Command-line Building With csc.exe. You can also build this example in [!INCLUDEvsprvs] by pasting the code into a new project. Also see How to: Compile and Run a Complete Windows Forms Code Example Using Visual Studio.

See Also

xref:System.Windows.Forms.ToolStripControlHost
ToolStrip Control Overview
ToolStrip Control Architecture
ToolStrip Technology Summary