HOW TO:使用 Windows Form 應用程式中的事件
Windows Form 應用程式中的一般狀況為使用控制項顯示表單,然後依據使用者所按的控制項,執行特定動作。 例如,當使用者在網頁中按一下表單中的 Button 控制項,會引發事件。 藉由處理事件,應用程式能為按一下按鈕的動作執行適當的應用程式邏輯。
如需 Windows Form 的詳細資訊,請參閱 Windows Form 使用者入門。
若要處理 Windows Form 上的按一下按鈕事件
建立具有 Button 控制項的 Windows Form。
Private WithEvents myButton As Button
private Button button;
private: Button^ button;
定義比對 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) { //... }
將事件處理常式方法加入至 Button 的 Click 事件。
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) 將產生類似此範例的程式碼,以進行事件架設 (Wiring)。
範例
下列程式碼範例會處理 Button 的 Click 事件,以變更 TextBox 的背景色彩。 以粗體字表示的項目是顯示事件處理常式和它如何連接到 Button 的 Click 事件。
撰寫此範例的程式碼時,並沒有使用視覺化設計工具 (Visual Designer) (例如 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),並加以編譯和執行。 例如,如果原始程式檔 (Source File) 的名稱為 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。