Procedura: eseguire il wrapping di un controllo Windows Form con ToolStripControlHost
Aggiornamento: novembre 2007
La classe ToolStripControlHost è progettata per attivare l'inclusione di controlli Windows Form arbitrari mediante l'utilizzo del costruttore ToolStripControlHost o mediante l'estensione della classe ToolStripControlHost stessa. È più facile eseguire il wrapping del controllo mediante estensione della classe ToolStripControlHost e implementazione di proprietà e metodi che espongono proprietà e metodi del controllo utilizzati di frequente. È inoltre possibile esporre eventi per il controllo al livello della classe ToolStripControlHost.
Per includere un controllo in un ToolStripControlHost per derivazione
Estendere ToolStripControlHost. Implementare un costruttore predefinito che chiama il costruttore della classe base passando il controllo desiderato.
' Call the base constructor passing in a MonthCalendar instance. Public Sub New() MyBase.New(New MonthCalendar()) End Sub
// Call the base constructor passing in a MonthCalendar instance. public ToolStripMonthCalendar() : base (new MonthCalendar()) { }
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à.
Public ReadOnly Property MonthCalendarControl() As MonthCalendar Get Return CType(Control, MonthCalendar) End Get End Property
public MonthCalendar MonthCalendarControl { get { return Control as MonthCalendar; } }
Esporre altre proprietà e altri metodi di utilizzo frequente del controllo di cui è stato eseguito il wrapping con proprietà e metodi nella classe estesa.
' 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
// 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); }
Se lo si desidera, eseguire l'override dei metodi OnSubscribeControlEvents e OnUnsubscribeControlEvents e aggiungere gli eventi di controllo che si desidera esporre.
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
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); }
Fornire il wrapping necessario per gli eventi che si desidera esporre.
' 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
// 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); } }
Esempio
'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
//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);
}
}
}
Compilazione del codice
Per questo esempio sono necessari i seguenti requisiti:
Riferimenti agli assembly System e System.Windows.Forms.
Per informazioni sulla generazione di questo esempio dalla riga di comando per Visual Basic o Visual C#, vedere Compilazione dalla riga di comando (Visual Basic) o Compilazione dalla riga di comando con csc.exe. È anche possibile generare questo esempio in Visual Studio incollando il codice in un nuovo progetto.
Vedere anche
Concetti
Architettura del controllo ToolStrip
Riepilogo della tecnologia ToolStrip