3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

イベントハンドラについての備忘録

Last updated at Posted at 2017-10-21

簡単なメモ書き。

複数イベントをまとめる方法など。

イベントハンドラの操作

public MainWindow()
{
    InitializeComponent();
 
    this.btn1.Click += btn_Click;
    this.btn2.Click += btn_Click;

    //直接メソッドを書く方法。
    this.btn3.Click += (sender, e) => MessageBox.Show("Button3だよ");
}


private void btn_Click(object sender, EventArgs e)
{
    Button btn = sender as Button;

    //ボタンの種類によって処理を変更
    switch (btn.Name)
    {
        case "btn1":
            MessageBox.Show("Button1だよ");
            break;

        case "btn2":
            MessageBox.Show("Button2だよ");
            break;
    }
}

ちなみにthis.btn1.Click += btn_Click;の部分では、
Buttonがデリゲート型のClickプロパティを持っており、
btn_Clickメソッドを委譲している。

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?