【Winform】カウンターボタンの作成チュートリアル
このチュートリアルでは、Windows Formsアプリケーションでカスタムボタンを作成し、クリックされた回数を表示するシンプルなカウンターボタンの作成から、外部からカウンタ値を変更する方法、さらにプロパティウィンドウでプロパティを操作できるようにする手順を順を追って解説します。
1. シンプルなカウンターボタンの作成
1.1 必要な名前空間の追加
まず、System.Windows.Forms名前空間を使用して、基本的なボタン機能を継承したカウンターボタンを作成します。
using System;
using System.Windows.Forms;1.2 カウンターボタンのクラス作成
次に、CounterButtonクラスを作成します。このクラスはButtonクラスを継承し、クリックするたびにカウントが増加し、そのカウントをボタンのテキストとして表示します。
public class CounterButton : Button
{
    private int clickCount = 0;
    public CounterButton()
    {
        this.Text = "クリック: 0";
        this.Click += CounterButton_Click;
    }
    private void CounterButton_Click(object sender, EventArgs e)
    {
        clickCount++;
        this.Text = $"クリック: {clickCount}";
    }
}1.3 コントロールの使用
このカウンターボタンをフォームに追加して使用することができます。以下はフォームのロードイベントでこのボタンを追加する例です。
private void Form1_Load(object sender, EventArgs e)
{
    CounterButton counterButton = new CounterButton();
    counterButton.Location = Point(50, 50);
    this.Controls.Add(counterButton);
}2. 外部からカウンタ値を操作できるようにする
次に、カウンターボタンのカウント値を外部から操作できるように、Counterプロパティを追加します。
2.1 Counterプロパティの追加
CounterButtonクラスにCounterプロパティを追加し、外部からカウント値を取得・設定できるようにします。
public class CounterButton : Button
{
    private int clickCount = 0;
    public int Counter
    {
        get { return clickCount; }
        set 
        { 
            clickCount = value;
            this.Text = $"クリック: {clickCount}";
        }
    }
    public CounterButton()
    {
        this.Text = "クリック: 0";
        this.Click += CounterButton_Click;
    }
    private void CounterButton_Click(object sender, EventArgs e)
    {
        Counter++;
    }
}2.2 プロパティの使用方法
外部からCounterプロパティを使用してカウント値を操作することができます。
2.2.1 カウント値の取得
int currentCount = counterButton.Counter;2.2.2 カウント値の設定
counterButton.Counter = 10;  // カウント値を10に設定3. プロパティウィンドウでカウンタ値を操作できるようにする
最後に、Visual StudioのプロパティウィンドウにCounterプロパティを表示させ、デザイン時にカウント値を設定できるようにします。
3.1 属性の追加
Counterプロパティに以下の属性を追加し、プロパティウィンドウに表示させます。
using System.ComponentModel;
public class CounterButton : Button
{
    private int clickCount = 0;
    [Browsable(true)]
    [Description("ボタンがクリックされた回数を取得または設定します。")]
    [Category("動作")]
    public int Counter
    {
        get { return clickCount; }
        set 
        { 
            clickCount = value;
            this.Text = $"クリック: {clickCount}";
        }
    }
    public CounterButton()
    {
        this.Text = "クリック: 0";
        this.Click += CounterButton_Click;
    }
    private void CounterButton_Click(object sender, EventArgs e)
    {
        Counter++;
    }
}3.2 動作確認
Visual StudioのデザインモードでCounterButtonをフォームに追加し、プロパティウィンドウを確認します。Counterプロパティが表示され、デザイン時にカウント値を設定できることを確認します。
フォームでボタンクリックイベントを処理したいときは、次のコードをフォームに追加することで可能です
// デザイナーで追加されたCounterButtonに対してイベントハンドラを追加
this.counterButton1.Click += FormCounterButton_Click;
// フォームのクラスで定義するイベントハンドラ
private void FormCounterButton_Click(object sender, EventArgs e)
{
    MessageBox.Show("CounterButtonがクリックされました!");
}終わりに
このチュートリアルでは、シンプルなカウンターボタンの作成から始まり、外部からカウンタ値を操作する方法、そしてプロパティウィンドウでプロパティを操作できるようにする手順について解説しました。これらの手順を通じて、カスタムコントロールの作成とその機能拡張の基本を学ぶことができます。
この資料は、初学者が段階的にスキルを身につけるためのものです。特に、ボタンの継承、プロパティの設定、Visual Studioデザインモードでの操作について理解を深めることができます。






ディスカッション
コメント一覧
まだ、コメントがありません