ControlDesigner.OnClick(DesignerRegionMouseEventArgs) 方法


当用户在设计时单击关联控件时由设计宿主调用。Called by the design host when the user clicks the associated control at design time.

 virtual void OnClick(System::Web::UI::Design::DesignerRegionMouseEventArgs ^ e);
protected virtual void OnClick (System.Web.UI.Design.DesignerRegionMouseEventArgs e);
abstract member OnClick : System.Web.UI.Design.DesignerRegionMouseEventArgs -> unit
override this.OnClick : System.Web.UI.Design.DesignerRegionMouseEventArgs -> unit
Protected Overridable Sub OnClick (e As DesignerRegionMouseEventArgs)



一个 DesignerRegionMouseEventArgs 对象,它指定了位置,还可能指定了用户单击的控件设计器区域。A DesignerRegionMouseEventArgs object that specifies the location and, possibly, the control designer region that the user clicked.


下面的代码示例演示如何在控件的可单击区域中对 OnClick 事件使用处理程序,并使用 DesignerRegionMouseEventArgs 对象标识所单击的区域。The following code example shows how to use a handler for the OnClick event in a clickable region of the control and use a DesignerRegionMouseEventArgs object to identify the region that is being clicked.

此代码示例是 EditableDesignerRegion 类的更大代码示例的一部分。This code example is part of a larger code example for the EditableDesignerRegion class.

// Handler for the Click event, which provides the region in the arguments.
protected override void OnClick(DesignerRegionMouseEventArgs e)
    if (e.Region == null)

    // If the clicked region is not a header, return
    if (e.Region.Name.IndexOf("Header") != 0)

    // Switch the current view if required
    if (e.Region.Name.Substring(6, 1) != myControl.CurrentView.ToString())
        myControl.CurrentView = int.Parse(e.Region.Name.Substring(6, 1));
' Handler for the Click event, which provides the region in the arguments.
Protected Overrides Sub OnClick(ByVal e As DesignerRegionMouseEventArgs)
    If IsNothing(e.Region) Then
    End If

    ' If the clicked region is not a header, return
    If e.Region.Name.IndexOf("Header") <> 0 Then
    End If

    ' Switch the current view if required
    If e.Region.Name.Substring(6, 1) <> myControl.CurrentView.ToString() Then
        myControl.CurrentView = Integer.Parse(e.Region.Name.Substring(6, 1))
    End If
End Sub


对于设计图面上的控件上的某些操作,由设计宿主引发 ViewEvent 事件。The ViewEvent event is raised by the design host for certain actions on a control in the design surface. 例如,以下条件引发事件:For example, an event is raised for the following conditions:

  • 用户单击控件。The user clicks the control.

  • 控件设计器将在设计图面上绘制。The control designer paints on the design surface.

  • 用户进入或退出控件的模板编辑模式。The user enters or exits template editing mode for the control.

ControlDesigner 类提供了一个用于处理 IControlDesignerView.ViewEvent 事件的默认委托。The ControlDesigner class supplies a default delegate to handle the IControlDesignerView.ViewEvent event. 派生自 ControlDesigner 的类将重写 OnClick 方法,以处理用户单击控件时引发的事件。Classes deriving from ControlDesigner override the OnClick method to process events that are raised when the user clicks a control.

使用 OnClick 方法,派生类可以处理事件,而无需附加委托。The OnClick method allows derived classes to handle the event without attaching a delegate. 这是在派生类中处理事件的首选技术。This is the preferred technique for handling the event in a derived class.

OnClick 方法的默认实现将返回,而不执行任何处理。The default implementation of the OnClick method returns without performing any processing.


重写从 ControlDesigner 类派生的类中的 OnClick(DesignerRegionMouseEventArgs) 方法,以处理控件上的设计时单击事件。Override the OnClick(DesignerRegionMouseEventArgs) method in a class deriving from the ControlDesigner class to handle design-time click events on the control. 如果 e 不为 null,则在 Region 属性中指定了单击的区域(如果有)。If e is not null, the clicked region, if any, is specified in the Region property.