Export (0) Print
Expand All

ToolStripControlHost Class

Hosts custom controls or Windows Forms controls.

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

public class ToolStripControlHost : ToolStripItem

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 : ToolStripControlHost
	{
		// Call the base constructor passing in a MonthCalendar instance. 
		public ToolStripMonthCalendar() : base (new MonthCalendar()) { }

		public MonthCalendar MonthCalendarControl
		{
			get
			{
				return Control as MonthCalendar;
			}
		}

		// Expose the MonthCalendar.FirstDayOfWeek as a property. 
		public Day FirstDayOfWeek
		{
			get
			{
				return MonthCalendarControl.FirstDayOfWeek;
			}
			set { value = MonthCalendarControl.FirstDayOfWeek; }
		}

		// Expose the AddBoldedDate method. 
		public void AddBoldedDate(DateTime dateToBold)
		{
			MonthCalendarControl.AddBoldedDate(dateToBold);
		}

		// Subscribe and unsubscribe the control events you wish to expose. 
		protected override void OnSubscribeControlEvents(Control c)
		{
			// Call the base so the base events are connected. 
			base.OnSubscribeControlEvents(c);

			// Cast the control to a MonthCalendar control.
			MonthCalendar monthCalendarControl = (MonthCalendar) c;

			// Add the event.
			monthCalendarControl.DateChanged +=
				new DateRangeEventHandler(OnDateChanged);
		}

		protected override void OnUnsubscribeControlEvents(Control c)
		{
			// Call the base method so the basic events are unsubscribed. 
			base.OnUnsubscribeControlEvents(c);

			// Cast the control to a MonthCalendar control.
			MonthCalendar monthCalendarControl = (MonthCalendar) c;

			// Remove the event.
			monthCalendarControl.DateChanged -=
				new DateRangeEventHandler(OnDateChanged);
		}

		// Declare the DateChanged event. 
		public event DateRangeEventHandler DateChanged;

		// Raise the DateChanged event. 
		private void OnDateChanged(object sender, DateRangeEventArgs e)
		{
			if (DateChanged != null)
			{
				DateChanged(this, e);
			}
		}
	}

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

Community Additions

ADD
Show:
© 2014 Microsoft