LoginSignup
0
0

More than 3 years have passed since last update.

C# - CheckBoxのサンプル コードべた書き(Visual Studio不使用)/ClickイベントとCheckedChangedイベントの違い

Last updated at Posted at 2021-02-27

目次: C# - Windows Formsでよく使うコントロールたち (Visual Studioなし環境向け) - Qiita

画面キャプチャ

image.png

ClickイベントとCheckedChangedイベントの違い

  • CheckedChangedイベントは、ユーザー操作だけでなく、プログラムからCheckBoxのCheckedプロパティを操作した場合もコールされる。
  • Clickイベントは、ユーザー操作からは呼ばれる。「クリック(Click)」という名称だが、スペースキーでChecked状態を操作しても呼んでくれる。プログラムからCheckBoxのCheckedプロパティを操作した場合はコールされない。

確認用コード

確認用コード

using System;
using System.Drawing;
using System.Windows.Forms;

class CheckBoxSample:Form
{
    CheckBox checkBox;

    CheckBoxSample()
    {
        Text = "CheckBox sample";

        Controls.Add(checkBox = new CheckBox(){
            Location = new Point(10, 10),
            Size = new Size(150, 25),
            Text = "sample",
        });


        // 用途に合わせて、どちらか一方を使うとよいかと思います。
        checkBox.CheckedChanged += CheckBoxCheckedChanged;
        checkBox.Click += CheckBoxClick;


        Button button;
        Controls.Add(button = new Button(){
            Location = new Point(10, 50),
            Size = new Size(150, 25),
            Text = "test",
        });
        button.Click += (s,e)=>{checkBox.Checked = !checkBox.Checked;};
    }


    // CheckedChangedイベントは、
    // ユーザー操作だけでなく、
    // プログラムからCheckBoxのCheckedプロパティを操作した場合もコールされる。
    void CheckBoxCheckedChanged(object sender, EventArgs e)
    {
        // window title
        if ( checkBox.Checked ) {
            Text = "Checked";
        }
        else {
            Text = "Not checked";
        }
    }


    // Clickイベントは、
    // ユーザー操作からは呼ばれる。
    // (「クリック(Click)」という名称だが、スペースキーでCheck状態を操作しても呼んでくれる。)
    // プログラムからCheckBoxのCheckedプロパティを操作した場合はコールされない。
    void CheckBoxClick(object sender, EventArgs e)
    {
        Console.WriteLine("clicked");
    }


    [STAThread]
    static void Main()
    {
        Application.Run(new CheckBoxSample());
    }
}

テンプレ

テンプレ

using System;
using System.Drawing;
using System.Windows.Forms;

class CheckBoxSample:Form
{
    CheckBox checkBox;

    CheckBoxSample()
    {
        Text = "CheckBox sample";

        Controls.Add(checkBox = new CheckBox(){
            Location = new Point(10, 10),
            Size = new Size(150, 25),
            Text = "sample",
        });

        // 用途に合わせて、どちらか一方を使うとよいかと思います。
        //checkBox.Click += CheckBoxClick;
        checkBox.CheckedChanged += CheckBoxCheckedChanged;
    }

    //void CheckBoxClick(object sender, EventArgs e)
    void CheckBoxCheckedChanged(object sender, EventArgs e)
    {
        // window title
        if ( checkBox.Checked ) {
            Text = "Checked";
        }
        else {
            Text = "Not checked";
        }
    }

    [STAThread]
    static void Main()
    {
        Application.Run(new CheckBoxSample());
    }
}

参考サイト

0
0
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
0
0