方法 : Windows フォーム アプリケーションでイベントを利用する
一般的に Windows フォーム アプリケーションでは、フォームにコントロールを表示し、ユーザーがクリックしたコントロールに応じて特定のアクションを実行します。 たとえば、ユーザーがフォーム内の Button コントロールをクリックしたときに、このコントロールによってイベントが発生します。 そのイベントを処理することで、アプリケーションでは、そのボタンのクリックに対して適切なアプリケーション ロジックを実行できます。
Windows フォームの詳細については、「Windows フォームについて」を参照してください。
Windows フォームのボタン クリック イベントを処理するには
Button コントロールのある Windows フォームを作成します。
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 など) では、この例と同様のコードを生成して、イベント接続を行います。
使用例
次のコード例では、Button の Click イベントを処理して TextBox の背景色を変更します。 太字で示した要素は、イベント ハンドラーと、そのイベント ハンドラーを Button の Click イベントに接続する方法を示しています。
この例のコードは、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 という名前になります。