如何:在 Windows 窗体应用程序中使用事件

Windows 窗体应用程序中的一种常见情况是显示带控件的窗体,然后根据用户单击的控件执行特定操作。 例如,当用户在窗体中单击 Button 控件时,该控件会引发一个事件。 通过处理该事件,应用程序可以针对该按钮单击操作执行适当的应用程序逻辑。

有关 Windows 窗体的更多信息,请参见 Windows 窗体入门

处理 Windows 窗体上的按钮单击事件

  1. 创建一个具有 Button 控件的 Windows 窗体。

    Private WithEvents myButton As Button
    
    private Button button;
    
    private:
        Button^ button;
    
  2. 定义一个与 Click 事件委托签名匹配的事件处理程序。 Click 事件为该委托类型使用 EventHandler 类,而为该事件数据使用 EventArgs 类。

    Private Sub Button_Click(sender As Object, e As EventArgs)
        '...
    End Sub
    
    private void Button_Click(object sender, EventArgs e)
    {
        //...
    }
    
    private:
        void Button_Click(Object^ sender, EventArgs^ e)
        {
            //...
        }
    
  3. 将事件处理程序方法添加到 ButtonClick 事件。

    AddHandler myButton.Click, AddressOf Me.Button_Click
    
    button.Click += new EventHandler(this.Button_Click);
    
    button->Click += gcnew EventHandler(this, &SnippetForm::Button_Click);
    

    备注

    设计器(如 Visual Studio 2005)将通过生成与下面的示例中的代码类似的代码来为您执行此事件连接。

示例

下面的代码示例处理 ButtonClick 事件以更改 TextBox 的背景色。 以粗体表示的元素显示了该事件处理程序以及它如何连结到 ButtonClick 事件。

此示例中的代码不是使用可视设计器(例如 Visual Studio 2005)编写的,并且只包含基本的编程元素。 如果您使用设计器,它将生成附加代码。

Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Drawing

Public Class MyForm
    Inherits Form
    Private box As TextBox
    Private WithEvents myButton As Button

    Public Sub New()
        box = New TextBox()
        box.BackColor = System.Drawing.Color.Cyan
        box.Size = New Size(100, 100)
        box.Location = New Point(50, 50)
        box.Text = "Hello"

        myButton = New Button()
        myButton.Location = New Point(50, 100)
        myButton.Text = "Click Me"

        AddHandler myButton.Click, AddressOf Me.Button_Click

        Controls.Add(box)
        Controls.Add(myButton)
    End Sub

    ' The event handler.
    Private Sub Button_Click(sender As Object, e As EventArgs)
        box.BackColor = System.Drawing.Color.Green
    End Sub

    ' The STAThreadAttribute indicates that Windows Forms uses the
    ' single-threaded apartment model.
    <STAThread> _
    Public Shared Sub Main()
        Application.Run(New MyForm())
    End Sub
End Class
using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;

public class MyForm : Form
{
    private TextBox box;
    private Button button;

    public MyForm() : base()
    {
        box = new TextBox();
        box.BackColor = System.Drawing.Color.Cyan;
        box.Size = new Size(100,100);
        box.Location = new Point(50,50);
        box.Text = "Hello";

        button = new Button();
        button.Location = new Point(50,100);
        button.Text = "Click Me";

        // To wire the event, create
        // a delegate instance and add it to the Click event.
        button.Click += new EventHandler(this.Button_Click);
        Controls.Add(box);
        Controls.Add(button);
    }

    // The event handler.
    private void Button_Click(object sender, EventArgs e)
    {
        box.BackColor = System.Drawing.Color.Green;
    }

    // The STAThreadAttribute indicates that Windows Forms uses the
    // single-threaded apartment model.
    [STAThread]
    public static void Main()
    {
        Application.Run(new MyForm());
    }
}
#using <System.dll>
#using <System.Windows.Forms.dll>
#using <System.Drawing.dll>

using namespace System;
using namespace System::ComponentModel;
using namespace System::Windows::Forms;

public ref class MyForm : Form
{
private:
    TextBox^ box;
    Button^ button;

public:
    MyForm() : Form()
    {
        box = gcnew TextBox();
        box->BackColor = System::Drawing::Color::Cyan;
        box->Size = System::Drawing::Size(100,100);
        box->Location = System::Drawing::Point(50,50);
        box->Text = "Hello";

        button = gcnew Button();
        button->Location = System::Drawing::Point(50,100);
        button->Text = "Click Me";

        // To wire the event, create
        // a delegate instance and add it to the Click event.
        button->Click += gcnew EventHandler(this, &MyForm::Button_Click);
        Controls->Add(box);
        Controls->Add(button);
    }

private:
    // The event handler.
    void Button_Click(Object^ sender, EventArgs^ e)
    {
        box->BackColor = System::Drawing::Color::Green;
    }

    // The STAThreadAttribute indicates that Windows Forms uses the
    // single-threaded apartment model.
public:
    [STAThread]
    static void Main()
    {
        Application::Run(gcnew MyForm());
    }
};

int main()
{
    MyForm::Main();
}

编译代码

将上面的代码保存到一个文件(对于 C# 文件,扩展名为 .cs,对于 Visual Basic 2005,扩展名为 .vb)中,进行编译,然后执行。 例如,如果源文件名为 WinEvents.cs(或 WinEvents.vb),请执行下面的命令。

vbc /r:System.DLL /r:System.Windows.Forms.dll /r:System.Drawing.dll WinEvents.vb
csc /r:System.DLL /r:System.Windows.Forms.dll /r:System.Drawing.dll WinEvents.vb
cl /clr:pure WinEvents.cpp

您的可执行文件将被命名为 WinEvents.exe。

请参见

概念

事件和委托

使用事件

引发事件

其他资源

处理和引发事件