Vorgehensweise: Verwenden eines ToolStripControlHost als Wrapper für ein Windows Forms-Steuerelement
ToolStripControlHost dient zum Aktivieren des Hostings von beliebigen Windows Forms-Steuerelementen mithilfe des ToolStripControlHost-Konstruktors oder durch die Erweiterung von ToolStripControlHost selbst. Es ist einfacher, das Steuerelement durch Erweitern von ToolStripControlHost und Implementieren der Eigenschaften und Methoden zu umschließen, die häufig verwendete Eigenschaften und Methoden des Steuerelements verfügbar machen. Sie können Ereignisse für das Steuerelement auch auf der ToolStripControlHost-Ebene verfügbar machen.
So hosten Sie ein Steuerelement in einem "ToolStripControlHost" durch Ableitung
Erweitern Sie ToolStripControlHost. Implementieren Sie einen parameterlosen Konstruktor, der einen Basisklassenkonstruktor aufruft, der das gewünschte Steuerelement übergibt.
// 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
Deklarieren Sie eine Eigenschaft mit demselben Typ wie für das umschlossene Steuerelement, und geben Sie
Control
als richtigen Typ des Steuerelements im Accessor der Eigenschaft zurück.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
Machen Sie andere häufig verwendete Eigenschaften und Methoden des umschlossenen Steuerelements mithilfe von Eigenschaften und Methoden in der erweiterten Klasse verfügbar.
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
Überschreiben Sie optional die Methoden OnSubscribeControlEvents und OnUnsubscribeControlEvents, und fügen Sie die bereitzustellenden Steuerelementereignisse hinzu.
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
Geben Sie die erforderlichen Wrapper für die Ereignisse an, die Sie verfügbar machen möchten.
// 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
Beispiel
//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
Kompilieren des Codes
Für dieses Beispiel benötigen Sie Folgendes:
- Verweise auf die Assemblys "System" und "System.Windows.Forms".
Siehe auch
.NET Desktop feedback