Control.Click Händelse

Definition

Inträffar när kontrollen klickas.

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

Händelsetyp

Exempel

I följande kodexempel visas Click händelsen i en händelsehanterare.

   // 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

Kommentarer

Händelsen Click skickar en EventArgs till dess händelsehanterare, så den anger bara att ett klick har inträffat. Om du behöver mer specifik musinformation (knapp, antal klick, hjulrotation eller plats) använder du MouseClick händelsen. Händelsen aktiveras dock MouseClick inte om klickningen orsakas av en annan åtgärd än musens, till exempel genom att trycka på RETUR-tangenten.

Ett dubbelklick bestäms av musinställningarna för användarens operativsystem. Användaren kan ange tiden mellan klick på en musknapp som ska betraktas som ett dubbelklick i stället för två klick. Händelsen Click aktiveras varje gång en kontroll dubbelklickas. Om du till exempel har händelsehanterare för Click händelserna och för DoubleClick en Formgenereras Click händelserna och DoubleClick när formuläret dubbelklickas och båda metoderna anropas. Om en kontroll dubbelklickas och den kontrollen inte stöder DoubleClick händelsen kan händelsen Click aktiveras två gånger.

Du måste ange StandardClick värdet ControlStyles för till true för att den här händelsen ska genereras.

Note

Följande händelser utlöses inte för TabControl klassen om det inte finns minst en TabPage i TabControl.TabPages samlingen: Click, , DoubleClickMouseDown, MouseUp, MouseHover, MouseEnteroch MouseLeaveMouseMove. Om det finns minst en TabPage i samlingen och användaren interagerar med flikkontrollens rubrik (där TabPage namnen visas) TabControl genererar den lämpliga händelsen. Men om användarinteraktionen finns inom klientområdet på fliksidan TabPage genererar den lämpliga händelsen.

Mer information om hur du hanterar händelser finns i Hantera och höja händelser.

Anteckningar till arvtagare

Att ärva från en standardkontroll Windows Forms och ändra StandardClick eller StandardDoubleClick värden för ControlStyles till true kan orsaka oväntat beteende eller inte ha någon effekt alls om kontrollen inte stöder Click eller DoubleClick händelser.

I följande tabell visas Windows Forms kontroller och vilken händelse (Click eller DoubleClick) som genereras som svar på den angivna musåtgärden.

Control Vänster musklickning Dubbelklicka på vänster mus Högerklicka Dubbelklicka med höger mus Musklick i mitten Dubbelklicka med mittenmus XButton1-musklickning XButton1-mus Double-Click XButton2-musklickning XButton2 Mouse Double-Click
MonthCalendar, DateTimePicker, , HScrollBarVScrollBar inget inget inget inget inget inget inget inget inget inget
Button, CheckBox, , RichTextBoxRadioButton Klicka på Klicka, klicka på inget inget inget inget inget inget inget inget
ListBox, , CheckedListBoxComboBox Klicka på Klicka, DoubleClick inget inget inget inget inget inget inget inget
TextBox, , DomainUpDownNumericUpDown Klicka på Klicka, DoubleClick inget inget inget inget inget inget inget inget
* TreeView, * ListView Klicka på Klicka, DoubleClick Klicka på Klicka, DoubleClick inget inget inget inget inget inget
ProgressBar, TrackBar Klicka på Klicka, klicka på Klicka på Klicka, klicka på Klicka på Klicka, klicka på Klicka på Klicka, klicka på Klicka på Klicka, klicka på
Form, DataGrid, Label, LinkLabel, Panel, GroupBox, PictureBox, Splitter, StatusBar, , ToolBar, TabPage, ** TabControl Klicka på Klicka, DoubleClick Klicka på Klicka, DoubleClick Klicka på Klicka, DoubleClick Klicka på Klicka, DoubleClick Klicka på Klicka, DoubleClick

* Muspekaren måste vara över ett underordnat objekt (TreeNode eller ListViewItem).

** Måste TabControl ha minst en TabPage i samlingen TabPages .

Gäller för

Se även