次の方法で共有


既存のコントロールを拡張する方法

既存のコントロールにさらに機能を追加する場合は、既存のコントロールから継承するコントロールを作成できます。 新しいコントロールには、基本コントロールのすべての機能と視覚的な側面が含まれていますが、拡張する機会が与えます。 たとえば、Buttonを継承するコントロールを作成した場合、新しいコントロールはボタンとまったく同じように見え、動作します。 新しいメソッドとプロパティを作成して、コントロールの動作をカスタマイズできます。 一部のコントロールでは、OnPaint メソッドをオーバーライドして、コントロールの外観を変更できます。

プロジェクトにカスタム コントロールを追加する

新しいプロジェクトを作成したら、Visual Studio テンプレートを使用してユーザー コントロールを作成します。 次の手順では、プロジェクトにユーザー コントロールを追加する方法を示します。

  1. Visual Studio で、Project Explorer ペインを見つけます。 プロジェクトを右クリックし、[ 追加>User コントロール] を選択します。

    Visual Studio ソリューション エクスプローラーを右クリックして、Windows フォーム プロジェクトにユーザー コントロールを追加

  2. カスタム コントロール (Windows フォーム) 項目を選択します。

  3. [ ボックスに、ユーザー コントロールの名前を入力します。 Visual Studio には、使用できる既定の一意の名前が用意されています。 次に、を押してを追加します。

    Visual Studio for Windows Forms の [項目の追加] ダイアログ

  4. コントロールの デザイン モードで、 F7 キーを押すか、 コード ビューへの切り替えリンクを クリックします。

    ヒント

    ソリューション エクスプローラー ウィンドウでファイルを右クリックし、[コードの表示] を選択することもできます。

カスタム コントロールをボタンに変更する

このセクションでは、クリック回数をカウントして表示するボタンにカスタム コントロールを変更する方法について説明します。

の .NET 用 Windows フォーム カスタム コントロール

プロジェクトに追加した後、コントロール デザイナーを開く必要があります。 そうでない場合は、ソリューション エクスプローラーのでコントロールをダブルクリックします。 カスタム コントロールを Button から継承して拡張するコントロールに変換するには、次の手順に従います。

  1. コントロール デザイナーを開いた状態で、F7 押すか、デザイナー ウィンドウを右クリックし、[コードの表示]選択します。

  2. コード エディターに、クラス定義が表示されます。

    namespace CustomControlProject
    {
        public partial class CustomControl2 : Control
        {
            public CustomControl2()
            {
                InitializeComponent();
            }
    
            protected override void OnPaint(PaintEventArgs pe)
            {
                base.OnPaint(pe);
            }
        }
    }
    
    Public Class CustomControl2
    
        Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
            MyBase.OnPaint(e)
    
            'Add your custom paint code here
        End Sub
    
    End Class
    
  3. 基底クラスを Control から Buttonに変更します。

    重要

    Visual Basic使用している場合、基本クラスはコントロールの *.designer.vb ファイルで定義されます。 Visual Basic で使用する基本クラスは System.Windows.Forms.Buttonです。

  4. _counterという名前のクラス スコープ変数を追加します。

    private int _counter = 0;
    
    Private _counter As Integer = 0
    
  5. OnPaint メソッドをオーバーライドします。 このメソッドは、コントロールを描画します。 コントロールはボタンの上に文字列を描画する必要があるため、最初に基底クラスの OnPaint メソッドを呼び出してから、文字列を描画する必要があります。

    protected override void OnPaint(PaintEventArgs pe)
    {
        // Draw the control
        base.OnPaint(pe);
    
        // Paint our string on top of it
        pe.Graphics.DrawString($"Clicked {_counter} times", Font, Brushes.Purple, new PointF(3, 3));
    }
    
    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
    
        ' Draw the control
        MyBase.OnPaint(e)
    
        ' Paint our string on top of it
        e.Graphics.DrawString($"Clicked {_counter} times", Font, Brushes.Purple, New PointF(3, 3))
    
    End Sub
    
  6. 最後に、OnClick メソッドをオーバーライドします。 このメソッドは、コントロールが押されるたびに呼び出されます。 コードはカウンターを増やし、Invalidate メソッドを呼び出します。これにより、コントロール自体が強制的に再描画されます。

    protected override void OnClick(EventArgs e)
    {
        // Increase the counter and redraw the control
        _counter++;
        Invalidate();
    
        // Call the base method to invoke the Click event
        base.OnClick(e);
    }
    
    Protected Overrides Sub OnClick(e As EventArgs)
    
        ' Increase the counter and redraw the control
        _counter += 1
        Invalidate()
    
        ' Call the base method to invoke the Click event
        MyBase.OnClick(e)
    
    End Sub
    

    最後のコードは次のスニペットのようになります。

    public partial class CustomControl1 : Button
    {
        private int _counter = 0;
    
        public CustomControl1()
        {
            InitializeComponent();
        }
    
        protected override void OnPaint(PaintEventArgs pe)
        {
            // Draw the control
            base.OnPaint(pe);
    
            // Paint our string on top of it
            pe.Graphics.DrawString($"Clicked {_counter} times", Font, Brushes.Purple, new PointF(3, 3));
        }
    
        protected override void OnClick(EventArgs e)
        {
            // Increase the counter and redraw the control
            _counter++;
            Invalidate();
    
            // Call the base method to invoke the Click event
            base.OnClick(e);
        }
    }
    
    Public Class CustomControl1
    
        Private _counter As Integer = 0
    
        Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
    
            ' Draw the control
            MyBase.OnPaint(e)
    
            ' Paint our string on top of it
            e.Graphics.DrawString($"Clicked {_counter} times", Font, Brushes.Purple, New PointF(3, 3))
    
        End Sub
    
        Protected Overrides Sub OnClick(e As EventArgs)
    
            ' Increase the counter and redraw the control
            _counter += 1
            Invalidate()
    
            ' Call the base method to invoke the Click event
            MyBase.OnClick(e)
    
        End Sub
    
    End Class
    

コントロールが作成されたら、プロジェクトをコンパイルして、ツールボックス ウィンドウに新しいコントロールを設定します。 フォーム デザイナーを開き、コントロールをフォームにドラッグします。 プロジェクトを実行し、ボタンを押します。 押すたびにクリック数が 1 つ増える。 クリックの合計は、ボタンの上部にテキストとして出力されます。

カスタム コントロールを表示する Windows フォームの Visual Studio ツールボックス ウィンドウ。