Procedura: eseguire il wrapping di un controllo Windows Form con ToolStripControlHostHow to: Wrap a Windows Forms Control with ToolStripControlHost

ToolStripControlHost è progettato per abilitare l'hosting di controlli Windows Form arbitrari usando il costruttore ToolStripControlHost o estendendo direttamente ToolStripControlHost.ToolStripControlHost is designed to enable hosting of arbitrary Windows Forms controls by using the ToolStripControlHost constructor or by extending ToolStripControlHost itself. È più semplice eseguire il wrapping del controllo estendendo ToolStripControlHost e implementando proprietà e metodi che espongono le proprietà e i metodi del controllo usati di frequente.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. È anche possibile esporre gli eventi del controllo a livello di ToolStripControlHost.You can also expose events for the control at the ToolStripControlHost level.

Per ospitare un controllo in un oggetto ToolStripControlHost tramite derivazioneTo host a control in a ToolStripControlHost by derivation

  1. Estendere ToolStripControlHost.Extend ToolStripControlHost. Implementare un costruttore predefinito che chiama il costruttore di classe base passando il controllo desiderato.Implement a default constructor that calls the base class constructor passing in the desired control.

    // Call the base constructor passing in a MonthCalendar instance.
    ToolStripMonthCalendar() : ToolStripControlHost( gcnew MonthCalendar ) {}
    
    // Call the base constructor passing in a MonthCalendar instance.
    public ToolStripMonthCalendar() : base (new MonthCalendar()) { }
    
    ' Call the base constructor passing in a MonthCalendar instance.
    Public Sub New() 
        MyBase.New(New MonthCalendar())
    
    End Sub
    
  2. Dichiarare una proprietà dello stesso tipo del controllo di cui è stato eseguito il wrapping e restituire Control come tipo corretto di controllo nella funzione di accesso della proprietà.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.

    property MonthCalendar^ MonthCalendarControl 
    {
       MonthCalendar^ get()
       {
          return static_cast<MonthCalendar^>(Control);
       }
    }
    
    public MonthCalendar MonthCalendarControl
    {
    	get
    	{
    		return Control as MonthCalendar;
    	}
    }
    
    Public ReadOnly Property MonthCalendarControl() As MonthCalendar 
        Get
            Return CType(Control, MonthCalendar)
        End Get
    End Property
    
  3. Esporre altre proprietà e metodi usati di frequente del controllo di cui è stato eseguito il wrapping con le proprietà e i metodi della classe estesa.Expose other frequently used properties and methods of the wrapped control with properties and methods in the extended class.

    property Day FirstDayOfWeek 
    {
       // Expose the MonthCalendar.FirstDayOfWeek as a property.
       Day get()
       {
          return MonthCalendarControl->FirstDayOfWeek;
       }
    
       void set( Day value )
       {
          MonthCalendarControl->FirstDayOfWeek = value;
       }
    }
    
    // Expose the AddBoldedDate method.
    void AddBoldedDate( DateTime dateToBold )
    {
       MonthCalendarControl->AddBoldedDate( dateToBold );
    }
    
    // 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);
    }
    
    ' Expose the MonthCalendar.FirstDayOfWeek as a property.
    Public Property FirstDayOfWeek() As Day 
        Get
            Return MonthCalendarControl.FirstDayOfWeek
        End Get
        Set
            MonthCalendarControl.FirstDayOfWeek = value
        End Set
    End Property
     
    ' Expose the AddBoldedDate method.
    Public Sub AddBoldedDate(ByVal dateToBold As DateTime) 
        MonthCalendarControl.AddBoldedDate(dateToBold)
    
    End Sub
    
  4. Facoltativamente, eseguire l'override dei metodi OnSubscribeControlEvents e OnUnsubscribeControlEvents e aggiungere gli eventi del controllo che si vuole esporre.Optionally, override the OnSubscribeControlEvents, and OnUnsubscribeControlEvents methods and add the control events you want to expose.

    void OnSubscribeControlEvents( System::Windows::Forms::Control^ c )
    {
       // Call the base so the base events are connected.
       __super::OnSubscribeControlEvents( c );
       
       // Cast the control to a MonthCalendar control.
       MonthCalendar^ monthCalendarControl = (MonthCalendar^)c;
       
       // Add the event.
       monthCalendarControl->DateChanged += gcnew DateRangeEventHandler( this, &ToolStripMonthCalendar::HandleDateChanged );
    }
    
    void OnUnsubscribeControlEvents( System::Windows::Forms::Control^ c )
    {
       
       // Call the base method so the basic events are unsubscribed.
       __super::OnUnsubscribeControlEvents( c );
       
       // Cast the control to a MonthCalendar control.
       MonthCalendar^ monthCalendarControl = (MonthCalendar^)c;
       
       // Remove the event.
       monthCalendarControl->DateChanged -= gcnew DateRangeEventHandler( this, &ToolStripMonthCalendar::HandleDateChanged );
    }
    
    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);
    }
    
    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
    
  5. Eseguire il wrapping necessario per gli eventi che si vuole esporre.Provide the necessary wrapping for the events you want to expose.

    // Declare the DateChanged event.
    // Raise the DateChanged event.
    void HandleDateChanged( Object^ sender, DateRangeEventArgs^ e )
    {
       if ( DateChanged != nullptr )
       {
          DateChanged( this, e );
       }
    }
    
    // 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 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
    

EsempioExample

//Declare a class that inherits from ToolStripControlHost.
public ref class ToolStripMonthCalendar: public ToolStripControlHost
{
public:
   // Call the base constructor passing in a MonthCalendar instance.
   ToolStripMonthCalendar() : ToolStripControlHost( gcnew MonthCalendar ) {}

   property MonthCalendar^ MonthCalendarControl 
   {
      MonthCalendar^ get()
      {
         return static_cast<MonthCalendar^>(Control);
      }
   }
   property Day FirstDayOfWeek 
   {
      // Expose the MonthCalendar.FirstDayOfWeek as a property.
      Day get()
      {
         return MonthCalendarControl->FirstDayOfWeek;
      }

      void set( Day value )
      {
         MonthCalendarControl->FirstDayOfWeek = value;
      }
   }

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

protected:
   // Subscribe and unsubscribe the control events you wish to expose.
   void OnSubscribeControlEvents( System::Windows::Forms::Control^ c )
   {
      // Call the base so the base events are connected.
      __super::OnSubscribeControlEvents( c );
      
      // Cast the control to a MonthCalendar control.
      MonthCalendar^ monthCalendarControl = (MonthCalendar^)c;
      
      // Add the event.
      monthCalendarControl->DateChanged += gcnew DateRangeEventHandler( this, &ToolStripMonthCalendar::HandleDateChanged );
   }

   void OnUnsubscribeControlEvents( System::Windows::Forms::Control^ c )
   {
      
      // Call the base method so the basic events are unsubscribed.
      __super::OnUnsubscribeControlEvents( c );
      
      // Cast the control to a MonthCalendar control.
      MonthCalendar^ monthCalendarControl = (MonthCalendar^)c;
      
      // Remove the event.
      monthCalendarControl->DateChanged -= gcnew DateRangeEventHandler( this, &ToolStripMonthCalendar::HandleDateChanged );
   }

public:
   event DateRangeEventHandler^ DateChanged;

private:
   // Declare the DateChanged event.
   // Raise the DateChanged event.
   void HandleDateChanged( Object^ sender, DateRangeEventArgs^ e )
   {
      if ( DateChanged != nullptr )
      {
         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);
		}
	}
}
'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
            MonthCalendarControl.FirstDayOfWeek = value
        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

Compilazione del codiceCompiling the Code

  • L'esempio presenta i requisiti seguenti:This example requires:

  • Riferimenti agli assembly System e System.Windows.Forms.References to the System and System.Windows.Forms assemblies.

Per informazioni sulla compilazione di questo esempio dalla riga di comando per Visual Basic o Visual c#, vedere compilazione dalla riga di comando oppure con la creazione della riga di comando csc.exe.For information about building this example from the command line for Visual Basic or Visual C#, see Building from the Command Line or Command-line Building With csc.exe. È anche possibile compilare questo esempio in Visual Studio incollando il codice in un nuovo progetto.You can also build this example in Visual Studio by pasting the code into a new project. Vedere anche Procedura: Compilare ed eseguire un esempio di codice Windows Form completo tramite Visual Studio.Also see How to: Compile and Run a Complete Windows Forms Code Example Using Visual Studio.

Vedere ancheSee Also

ToolStripControlHost
Panoramica sul controllo ToolStripToolStrip Control Overview
Architettura del controllo ToolStripToolStrip Control Architecture
Riepilogo della tecnologia ToolStripToolStrip Technology Summary