Files
docs-desktop/dotnet-desktop-guide/framework/winforms/controls/how-to-wrap-a-windows-forms-control-with-toolstripcontrolhost.md
Andy De George c0ba284473 Initial winforms content migrated (#18)
* Merge winforms framework content to working branch (#14)

* Breadcrumb / TOC / Move net5 to net folder (#15)

* change path from net5 to net

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Add .net 5 winforms placeholder article (#16)

* added some metadata and adjusted net5 placeholder

* corrections

* corrections

* corrections

* Test1

* Swapping landing page vs concept

* fix links

* Fix links

* Fix desc
2020-09-01 16:26:21 -07:00

5.2 KiB

title, ms.date, dev_langs, helpviewer_keywords, ms.assetid
title ms.date dev_langs helpviewer_keywords ms.assetid
Wrap Control with ToolStripControlHost 03/30/2017
csharp
vb
cpp
ToolStrip control [Windows Forms], wrapping controls
toolbars [Windows Forms], wrapping controls
ToolStrip control [Windows Forms], hosting controls
e2ce4990-661d-4882-a116-8a9eb575dc84

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 parameterless 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.

See also