HOW TO:使用 ToolStripControlHostWrap 為 Windows Form 控制項換行
更新:2007 年 11 月
ToolStripControlHost 的設計目的是利用 ToolStripControlHost 建構函式或者藉由擴充 ToolStripControlHost 本身,來啟用任意 Windows Form 控制項裝載。擴充 ToolStripControlHost 並且實作那些控制項屬性和方法所公開常用的屬性和方法,是較簡單的包裝控制項方法。也可以在 ToolStripControlHost 層級上公開控制項的事件。
若要藉由衍生在 ToolStripControlHost 中裝載控制項
擴充 ToolStripControlHost。實作預設的建構函式,此建構函式會呼叫傳遞所需控制項中的基底類別建構函式。
' 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()) { }
宣告同一型別的屬性為包裝控制項 (Wrapped Control),並且傳回 Control 當做屬性存取子中的正確型別控制項。
Public ReadOnly Property MonthCalendarControl() As MonthCalendar Get Return CType(Control, MonthCalendar) End Get End Property
public MonthCalendar MonthCalendarControl { get { return Control as MonthCalendar; } }
使用擴充類別 (Extended Class) 上的屬性和方法來公開包裝控制項經常使用的其他屬性和方法。
' 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); }
請選擇性覆寫 OnSubscribeControlEvents 和 OnUnsubscribeControlEvents 方法並加入要公開的控制項事件。
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); }
請為要公開的事件提供必要的包裝。
' 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); } }
範例
'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);
}
}
}
編譯程式碼
這個範例需要:
System 和 System.Windows.Forms 組件的參考。
如需從 Visual Basic 或 Visual C# 的命令列建置這個範例的詳細資訊,請參閱從命令列建置 (Visual Basic) 或使用 csc.exe 建置命令列。您也可以透過將程式碼貼入新的專案,在 Visual Studio 中建置此範例。