Control.Click 事件
在单击控件时发生。
**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)
语法
声明
Public Event Click As EventHandler
用法
Dim instance As Control
Dim handler As EventHandler
AddHandler instance.Click, handler
public event EventHandler Click
public:
event EventHandler^ Click {
void add (EventHandler^ value);
void remove (EventHandler^ value);
}
/** @event */
public void add_Click (EventHandler value)
/** @event */
public void remove_Click (EventHandler value)
JScript 支持使用事件,但不支持进行新的声明。
备注
Click 事件将 EventArgs 传递给其事件处理程序,所以它仅指示发生了一次单击。如果需要更具体的鼠标信息(按钮、单击次数、滚轮旋转或位置),请使用 MouseClick 事件。但是,如果导致单击的操作不是鼠标操作(例如,由按 Enter 键导致的),将不会引发 MouseClick 事件。
双击操作由用户操作系统的鼠标设置确定。用户可以设置两次单击鼠标按钮之间的时间以便将这两次单击认为是双击而不是两次单击。每当双击控件时,就会引发 Click 事件。例如,如果您有 Form 的 Click 和 DoubleClick 事件的事件处理程序,则当双击该窗体并同时调用这两个方法时,会引发 Click 和 DoubleClick 事件。如果双击一个控件并且该控件不支持 DoubleClick 事件,则 Click 事件可能被引发两次。
若要引发该事件,必须将 ControlStyles 的 StandardClick 值设置为 true。
提示
除非 TabControl.TabPages 集合中至少有一个 TabPage,否则不会引发 TabControl 类的下列事件:Click、DoubleClick、MouseDown、MouseUp、MouseHover、MouseEnter、MouseLeave 和 MouseMove。如果集合中至少有一个 TabPage,并且用户与选项卡控件标头(显示 TabPage 名称的地方)交互,则 TabControl 将引发相应的事件。但是,如果用户交互发生在选项卡页的工作区内,则 TabPage 将引发相应的事件。
有关处理事件的更多信息,请参见 使用事件。
给继承者的说明 如果标准 Windows 窗体控件不支持 Click 或 DoubleClick 事件,则从该控件继承并将 ControlStyles 的 StandardClick 或 StandardDoubleClick 值更改为 true 会导致意外的行为,或者根本不会产生任何效果。 下表列出 Windows 窗体控件及指定的鼠标操作会引发哪个事件(Click 还是 DoubleClick)。
控件 |
Left Mouse Click |
Left Mouse Double Click |
Right Mouse Click |
Right Mouse Double Click |
Middle Mouse Click |
Middle Mouse Double Click |
XButton1 Mouse Click |
XButton1 Mouse Double-Click |
XButton2 Mouse Click |
XButton2 Mouse Double-Click |
---|---|---|---|---|---|---|---|---|---|---|
无 |
无 |
无 |
无 |
无 |
无 |
无 |
无 |
无 |
无 |
|
Click |
Click、Click |
无 |
无 |
无 |
无 |
无 |
无 |
无 |
无 |
|
Click |
Click、DoubleClick |
无 |
无 |
无 |
无 |
无 |
无 |
无 |
无 |
|
Click |
Click、DoubleClick |
无 |
无 |
无 |
无 |
无 |
无 |
无 |
无 |
|
* TreeView, * ListView |
Click |
Click、DoubleClick |
Click |
Click、DoubleClick |
无 |
无 |
无 |
无 |
无 |
无 |
Click |
Click、Click |
Click |
Click、Click |
Click |
Click、Click |
Click |
Click、Click |
Click |
Click、Click |
|
Form, TabPage, ** TabControl |
Click |
Click、DoubleClick |
Click |
Click、DoubleClick |
Click |
Click、DoubleClick |
Click |
Click、DoubleClick |
Click |
Click、DoubleClick |
* 鼠标指针必须位于子对象上(TreeNode 或 ListViewItem)。 TabControl 的 TabPages 集合中必须至少有一个 TabPage。
示例
下面的代码示例显示事件处理程序中的 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 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
// 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 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.get_Parent();
// Set the text and backcolor of the parent control.
control.set_Text("My Groupbox");
control.set_BackColor(Color.get_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.set_Text("The Form of My Control");
myForm.set_BackColor(Color.get_Red());
} //button1_Click
平台
Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
版本信息
.NET Framework
受以下版本支持:2.0、1.1、1.0
.NET Compact Framework
受以下版本支持:2.0、1.0
请参见
参考
Control 类
Control 成员
System.Windows.Forms 命名空间
OnClick
StandardClick
MouseClick
DoubleClick
MouseDoubleClick