This documentation is archived and is not being maintained.

ToolStripControlHost Class

Hosts custom controls or Windows Forms controls.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

'Declaration
Public Class ToolStripControlHost _
	Inherits ToolStripItem
'Usage
Dim instance As ToolStripControlHost

ToolStripControlHost is the abstract base class for ToolStripComboBox, ToolStripTextBox, and ToolStripProgressBar. ToolStripControlHost can host other controls, including custom controls, in two ways:

  • Construct a ToolStripControlHost with a class that derives from Control. To fully access the hosted control and properties, you must cast the Control property back to the actual class it represents.

  • Extend ToolStripControlHost, and in the inherited class's default constructor, call the base class constructor passing a class that derives from Control. This option lets you wrap common control methods and properties for easy access in a ToolStrip.

Use the ToolStripControlHost class to host your customized controls or any other Windows Forms control.

To customize a ToolStripItem, derive from ToolStripControlHost and create a custom implementation. You can override methods such as OnSubscribeControlEvents to handle events raised by the hosted controls, and you can put custom functionality into properties to enhance the hosted control.

The following code example demonstrates constructing a ToolStripControlHost with a MonthCalendar control, using OnSubscribeControlEvents to handle events, and exposing some of its members to the ToolStripControlHost.

'Declare a class that inherits from ToolStripControlHost. 

Public Class ToolStripMonthCalendar
    Inherits ToolStripControlHost

    ' Call the base constructor passing in a MonthCalendar instance. 
    Public Sub New() 
        MyBase.New(New MonthCalendar())

    End Sub 

    Public ReadOnly Property MonthCalendarControl() As MonthCalendar 
        Get 
            Return CType(Control, MonthCalendar)
        End Get 
    End Property 

    ' Expose the MonthCalendar.FirstDayOfWeek as a property. 
    Public Property FirstDayOfWeek() As Day 
        Get 
            Return MonthCalendarControl.FirstDayOfWeek
        End Get 
        Set
            value = MonthCalendarControl.FirstDayOfWeek
        End Set 
    End Property 

    ' Expose the AddBoldedDate method. 
    Public Sub AddBoldedDate(ByVal dateToBold As DateTime) 
        MonthCalendarControl.AddBoldedDate(dateToBold)

    End Sub 

    ' Subscribe and unsubscribe the control events you wish to expose. 
    Protected Overrides Sub OnSubscribeControlEvents(ByVal c As Control) 

        ' Call the base so the base events are connected. 
        MyBase.OnSubscribeControlEvents(c)

        ' Cast the control to a MonthCalendar control. 
        Dim monthCalendarControl As MonthCalendar = _
            CType(c, MonthCalendar)

        ' Add the event. 
        AddHandler monthCalendarControl.DateChanged, _
            AddressOf HandleDateChanged

    End Sub 

    Protected Overrides Sub OnUnsubscribeControlEvents(ByVal c As Control)
        ' Call the base method so the basic events are unsubscribed. 
        MyBase.OnUnsubscribeControlEvents(c)

        ' Cast the control to a MonthCalendar control. 
        Dim monthCalendarControl As MonthCalendar = _
            CType(c, MonthCalendar)

        ' Remove the event. 
        RemoveHandler monthCalendarControl.DateChanged, _
            AddressOf HandleDateChanged

    End Sub 

    ' Declare the DateChanged event. 
    Public Event DateChanged As DateRangeEventHandler

    ' Raise the DateChanged event. 
    Private Sub HandleDateChanged(ByVal sender As Object, _
        ByVal e As DateRangeEventArgs)

        RaiseEvent DateChanged(Me, e)
    End Sub 
End Class

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0
Show: