Control.Click 事件

定义

在单击控件时发生。Occurs when the control is clicked.

public:
 event EventHandler ^ Click;
public event EventHandler Click;
member this.Click : EventHandler 
Public Custom Event Click As EventHandler 

示例

下面的代码示例演示事件处理程序中的 Click 事件。The following code example shows the Click event in an event handler.

   // This example uses the Parent property and the Find method of Control to set
   // properties on the parent control of a Button and its Form. The example assumes
   // that a Button control named button1 is located within a GroupBox control. The 
   // example also assumes that the Click event of the Button control is connected to
   // the event handler method defined in the example.
private:
   void button1_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      // Get the control the Button control is located in. In this case a GroupBox.
      Control^ control = button1->Parent;
      
      // Set the text and backcolor of the parent control.
      control->Text = "My Groupbox";
      control->BackColor = Color::Blue;
      
      // Get the form that the Button control is contained within.
      Form^ myForm = button1->FindForm();
      
      // Set the text and color of the form containing the Button.
      myForm->Text = "The Form of My Control";
      myForm->BackColor = Color::Red;
   }
// This example uses the Parent property and the Find method of Control to set
// properties on the parent control of a Button and its Form. The example assumes
// that a Button control named button1 is located within a GroupBox control. The 
// example also assumes that the Click event of the Button control is connected to
// the event handler method defined in the example.
private void button1_Click(object sender, System.EventArgs e)
{
   // Get the control the Button control is located in. In this case a GroupBox.
   Control control = button1.Parent;
   // Set the text and backcolor of the parent control.
   control.Text = "My Groupbox";
   control.BackColor = Color.Blue;
   // Get the form that the Button control is contained within.
   Form myForm = button1.FindForm();
   // Set the text and color of the form containing the Button.
   myForm.Text = "The Form of My Control";
   myForm.BackColor = Color.Red;
}
' This example uses the Parent property and the Find method of Control to set
' properties on the parent control of a Button and its Form. The example assumes
' that a Button control named button1 is located within a GroupBox control. The 
' example also assumes that the Click event of the Button control is connected to
' the event handler method defined in the example.
Private Sub button1_Click(sender As Object, e As System.EventArgs) Handles button1.Click
   ' Get the control the Button control is located in. In this case a GroupBox.
   Dim control As Control = button1.Parent
   ' Set the text and backcolor of the parent control.
   control.Text = "My Groupbox"
   control.BackColor = Color.Blue
   ' Get the form that the Button control is contained within.
   Dim myForm As Form = button1.FindForm()
   ' Set the text and color of the form containing the Button.
   myForm.Text = "The Form of My Control"
   myForm.BackColor = Color.Red
End Sub

注解

Click 事件会将 EventArgs 传递给其事件处理程序,因此它只指示发生了单击操作。The Click event passes an EventArgs to its event handler, so it only indicates that a click has occurred. 如果需要更具体的鼠标信息(按钮、点击数、滚轮旋转或位置),请使用 MouseClick 事件。If you need more specific mouse information (button, number of clicks, wheel rotation, or location), use the MouseClick event. 但是,如果单击是由除鼠标之外的操作引起的,则不会引发 MouseClick 事件,如按 ENTER 键。However, the MouseClick event will not be raised if the click is caused by action other than that of the mouse, such as pressing the ENTER key.

双击由用户操作系统的鼠标设置确定。A double-click is determined by the mouse settings of the user's operating system. 用户可以设置两次单击鼠标按钮之间的时间以便将这两次单击认为是双击而不是两次单击。The user can set the time between clicks of a mouse button that should be considered a double-click rather than two clicks. 每次双击控件时都会引发 Click 事件。The Click event is raised every time a control is double-clicked. 例如,如果您具有 FormClickDoubleClick 事件的事件处理程序,则在双击窗体并调用这两个方法时,将引发 ClickDoubleClick 事件。For example, if you have event handlers for the Click and DoubleClick events of a Form, the Click and DoubleClick events are raised when the form is double-clicked and both methods are called. 如果双击控件,而该控件不支持 DoubleClick 事件,则 Click 事件可能会引发两次。If a control is double-clicked and that control does not support the DoubleClick event, the Click event might be raised twice.

必须将 ControlStylesStandardClick 值设置为 "true,以便引发此事件。You must set the StandardClick value of ControlStyles to true for this event to be raised.

备注

除非 TabControl.TabPages 集合中至少有一个 TabPage,否则不会为 TabControl 类引发以下事件: ClickDoubleClickMouseDownMouseUpMouseHoverMouseEnterMouseLeaveMouseMoveThe following events are not raised for the TabControl class unless there is at least one TabPage in the TabControl.TabPages collection: Click, DoubleClick, MouseDown, MouseUp, MouseHover, MouseEnter, MouseLeave and MouseMove. 如果集合中至少有一个 TabPage,并且用户与选项卡控件的标头(显示 TabPage 名称)交互,TabControl 将引发相应的事件。If there is at least one TabPage in the collection, and the user interacts with the tab control's header (where the TabPage names appear), the TabControl raises the appropriate event. 但是,如果用户交互位于选项卡页的工作区中,则 TabPage 会引发相应的事件。However, if the user interaction is within the client area of the tab page, the TabPage raises the appropriate event.

有关处理事件的详细信息,请参阅处理和引发事件For more information about handling events, see Handling and Raising Events.

对继承者的说明Notes to inheritors

如果控件不支持 trueClick 事件,从标准 Windows 窗体控件继承并将 ControlStylesStandardClickStandardDoubleClick 值更改为 DoubleClick 可能会导致意外的行为或根本不会有任何影响。Inheriting from a standard Windows Forms control and changing the StandardClick or StandardDoubleClick values of ControlStyles to true can cause unexpected behavior or have no effect at all if the control does not support the Click or DoubleClick events.

下表列出 Windows 窗体控件以及为响应指定的鼠标操作而引发的事件(ClickDoubleClick)。The following table lists Windows Forms controls and which event (Click or DoubleClick) is raised in response to the mouse action specified.

控件Control 单击鼠标左键Left Mouse Click 双击鼠标左键Left Mouse Double Click 鼠标右键单击Right Mouse Click 右键单击鼠标右键Right Mouse Double Click 鼠标中键单击Middle Mouse Click 双击鼠标中键Middle Mouse Double Click XButton1 鼠标单击XButton1 Mouse Click XButton1 鼠标双击XButton1 Mouse Double-Click XButton2 鼠标单击XButton2 Mouse Click XButton2 鼠标双击XButton2 Mouse Double-Click
MonthCalendar, DateTimePicker, HScrollBar, VScrollBarMonthCalendar, DateTimePicker, HScrollBar, VScrollBar none none none none none none none none none none
Button, CheckBox, RichTextBox, RadioButtonButton, CheckBox, RichTextBox, RadioButton 单击Click 单击,单击Click, Click none none none none none none none none
ListBox, CheckedListBox, ComboBoxListBox, CheckedListBox, ComboBox 单击Click 单击,DoubleClickClick, DoubleClick none none none none none none none none
TextBox, DomainUpDown, NumericUpDownTextBox, DomainUpDown, NumericUpDown 单击Click 单击,DoubleClickClick, DoubleClick none none none none none none none none
* TreeView,* ListView* TreeView, * ListView 单击Click 单击,DoubleClickClick, DoubleClick 单击Click 单击,DoubleClickClick, DoubleClick none none none none none none
ProgressBarTrackBarProgressBar, TrackBar 单击Click 单击,单击Click, Click 单击Click 单击,单击Click, Click 单击Click 单击,单击Click, Click 单击Click 单击,单击Click, Click 单击Click 单击,单击Click, Click
FormDataGridLabelLinkLabelPanelGroupBoxPictureBoxSplitterStatusBarToolBarTabPage、** TabControlForm, DataGrid, Label, LinkLabel, Panel, GroupBox, PictureBox, Splitter, StatusBar, ToolBar, TabPage, ** TabControl 单击Click 单击,DoubleClickClick, DoubleClick 单击Click 单击,DoubleClickClick, DoubleClick 单击Click 单击,DoubleClickClick, DoubleClick 单击Click 单击,DoubleClickClick, DoubleClick 单击Click 单击,DoubleClickClick, DoubleClick

* 鼠标指针必须位于子对象上(TreeNodeListViewItem)。* The mouse pointer must be over a child object (TreeNode or ListViewItem).

* * TabControlTabPages 集合中必须至少有一个 TabPage** The TabControl must have at least one TabPage in its TabPages collection.

适用于

另请参阅