ToolStripControlHost.OnSubscribeControlEvents(Control) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
從裝載控制項訂閱事件。
protected:
virtual void OnSubscribeControlEvents(System::Windows::Forms::Control ^ control);
protected virtual void OnSubscribeControlEvents (System.Windows.Forms.Control control);
protected virtual void OnSubscribeControlEvents (System.Windows.Forms.Control? control);
abstract member OnSubscribeControlEvents : System.Windows.Forms.Control -> unit
override this.OnSubscribeControlEvents : System.Windows.Forms.Control -> unit
Protected Overridable Sub OnSubscribeControlEvents (control As Control)
參數
- control
- Control
要訂閱事件的來源控制項。
範例
下列程式碼範例示範如何為主控控制項所公開的事件設定事件處理。 此程式碼範例是針對 類別提供的較大範例的 ToolStripControlHost 一部分。
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 );
}
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 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
備註
OnSubscribeControlEvents覆寫 方法以新增或防止同步控制事件。
如果您在這裡覆寫並連結事件,請使用 OnUnsubscribeControlEvents 方法將其取消攔截。
引發事件會透過委派叫用此事件處理常式。 如需詳細資訊,請參閱 處理和引發事件。
OnSubscribeControlEvents 方法也允許衍生類別處理事件,而不用附加委派。 這是在衍生類別中處理事件的慣用技巧。
給繼承者的注意事項
當在衍生類別中覆寫 OnSubscribeControlEvents(Control) 時,請確定呼叫基底類別的 OnSubscribeControlEvents(Control) 方法,使已註冊的委派能接收到事件。