Expand Minimize

How to: Wrap a Windows Forms Control with ToolStripControlHost

ToolStripControlHost is designed to enable hosting of arbitrary Windows Forms controls by using the ToolStripControlHost constructor or by extending ToolStripControlHost itself. It is easier to wrap the control by extending 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 ToolStripControlHost level.

To host a control in a ToolStripControlHost by derivation

  1. Extend ToolStripControlHost. Implement a default constructor that calls the base class constructor passing in the desired control.

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

    
    		public MonthCalendar MonthCalendarControl
    		{
    			get
    			{
    				return Control as MonthCalendar;
    			}
    		}
    
    
    
  3. Expose other frequently used properties and methods of the wrapped control with properties and methods in the extended class.

    
    		// Expose the MonthCalendar.FirstDayOfWeek as a property.
    		public Day FirstDayOfWeek
    		{
    			get
    			{
    				return MonthCalendarControl.FirstDayOfWeek;
    			}
    			set { MonthCalendarControl.FirstDayOfWeek = value; }
    		}
    
    		// Expose the AddBoldedDate method.
    		public void AddBoldedDate(DateTime dateToBold)
    		{
    			MonthCalendarControl.AddBoldedDate(dateToBold);
    		}
    
    
    
  4. Optionally, override the OnSubscribeControlEvents, and OnUnsubscribeControlEvents methods and add the control events you want 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);
    		}
    
    
    
  5. Provide the necessary wrapping for the events you want to expose.

    
    		// 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);
    			}
    		}
    
    
    


	//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 { MonthCalendarControl.FirstDayOfWeek = value; }
		}

		// 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);
			}
		}
	}


  • This example requires:

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

For information about building this example from the command line for Visual Basic or Visual C#, see Building from the Command Line (Visual Basic) or Command-line Building With csc.exe. You can also build this example in Visual Studio by pasting the code into a new project. For more information, see How to: Compile and Run a Complete Windows Forms Code Example Using Visual Studio and How to: Compile and Run a Complete Windows Forms Code Example Using Visual Studio and How to: Compile and Run a Complete Windows Forms Code Example Using Visual Studio and How to: Compile and Run a Complete Windows Forms Code Example Using Visual Studio and How to: Compile and Run a Complete Windows Forms Code Example Using Visual Studio.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft