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

更新:2007 年 11 月

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

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

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

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

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

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

    button.Click += new EventHandler(this.Button_Click);
    
    AddHandler myButton.Click, AddressOf Me.Button_Click
    
    说明:

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

示例

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

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

[C#]

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.
   [STAThreadAttribute]
   public static void Main(string[] args) 
   {
      Application.Run(new MyForm());
   }
}

[Visual Basic]

Option Strict On

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.
   <STAThreadAttribute()> _
   Public Shared Sub Main(args() As String)
      Application.Run(New MyForm())
   End Sub
End Class

编译代码

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

csc /r:System.DLL /r:System.Windows.Forms.DLL /r:System.Drawing.DLL WinEvents.cs
vbc /r:System.DLL /r:System.Windows.Forms.DLL /r:System.Drawing.DLL WinEvents.vb

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

请参见

概念

事件和委托

使用事件

引发事件

其他资源

处理和引发事件