Control.Click 事件

定義

發生於按下控制項時。

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

事件類型

範例

下列程式碼範例顯示 Click 事件處理常式中的 事件。

   // 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 至其事件處理常式,因此只會指出已按一下。 如果您需要更明確的滑鼠資訊 (按鈕、按一下次數、滾輪旋轉或位置) ,請使用 MouseClick 事件。 不過, MouseClick 如果按一下是由滑鼠以外的動作所造成,例如按下 ENTER 鍵,則不會引發事件。

按兩下是由使用者作業系統的滑鼠設定所決定。 使用者可以設定滑鼠按鍵的按一下之間應間隔多少時間才視為按兩下,而不是兩次按一下。 Click每次按兩下控制項時,就會引發 事件。 例如,如果您有 的 和 事件的事件處理常式 ClickClick 則會在按兩下表單並 DoubleClick 呼叫這兩種方法時引發 和 DoubleClick 事件。 Form 如果按兩下控制項,且該控制項不支援 DoubleClick 事件, Click 則事件可能會引發兩次。

您必須將 的值 ControlStylestrue 設定為 StandardClick ,才能引發此事件。

注意

除非集合 Click 中至少有一個 TabPage :、 DoubleClick 、、 MouseDown 、、 MouseEnterMouseHoverMouseUpMouseLeaveMouseMove ,否則不會針對 TabControl 類別引發下列 TabControl.TabPages 事件。 如果集合中至少有一個 TabPage ,而且使用者與索引標籤控制項的標頭互動 (TabPage 名稱出現在) ,則會 TabControl 引發適當的事件。 不過,如果使用者互動是在索引標籤頁面的工作區內,則會 TabPage 引發適當的事件。

如需處理事件的詳細資訊,請參閱 處理和引發事件

繼承者注意事項

繼承自標準Windows Forms控制項,並將 的 或 值變更 StandardClicktrue 可能會導致非預期的行為,如果控制項不支援 ClickDoubleClick 事件,則 ControlStyles 完全沒有作用。 StandardDoubleClick

下表列出Windows Forms控制項,以及引發哪些事件 (ClickDoubleClick) ,以回應指定的滑鼠動作。

控制 滑鼠左鍵按一下 滑鼠左鍵按兩下 按一下滑鼠右鍵 滑鼠右鍵按兩下 按一下滑鼠中間鍵 滑鼠中間鍵按兩下 XButton1 滑鼠按一下 XButton1 滑鼠Double-Click XButton2 滑鼠按一下 XButton2 滑鼠Double-Click
MonthCalendar, DateTimePicker, HScrollBar, VScrollBar
Button, CheckBox, RichTextBox, RadioButton 按一下 按一下,按一下
ListBox, CheckedListBox, ComboBox 按一下 按一下,DoubleClick
TextBox, DomainUpDown, NumericUpDown 按一下 按一下,DoubleClick
* TreeView, * ListView 按一下 按一下,DoubleClick 按一下 按一下,DoubleClick
ProgressBar, TrackBar 按一下 按一下,按一下 按一下 按一下,按一下 按一下 按一下,按一下 按一下 按一下,按一下 按一下 按一下,按一下
Form, DataGrid, Label, LinkLabel, Panel, GroupBox, PictureBox, Splitter, StatusBar, ToolBar, TabPage, ** TabControl 按一下 按一下,DoubleClick 按一下 按一下,DoubleClick 按一下 按一下,DoubleClick 按一下 按一下,DoubleClick 按一下 按一下 [DoubleClick]

* 滑鼠指標必須位於子物件上方, (TreeNodeListViewItem) 。

** 集合 TabControlTabPages 必須至少有一個 TabPage

適用於

另請參閱