ToolStripControlHost.OnSubscribeControlEvents(Control) 方法

定义

向托管控件订阅事件。

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) 方法,以便已注册的委托对事件进行接收。

适用于