既存のコントロールにさらに機能を追加する場合は、既存のコントロールから継承するコントロールを作成できます。 新しいコントロールには、基本コントロールのすべての機能と視覚的な側面が含まれていますが、拡張する機会が与えます。 たとえば、Buttonを継承するコントロールを作成した場合、新しいコントロールはボタンとまったく同じように見え、動作します。 新しいメソッドとプロパティを作成して、コントロールの動作をカスタマイズできます。 一部のコントロールでは、OnPaint メソッドをオーバーライドして、コントロールの外観を変更できます。
プロジェクトにカスタム コントロールを追加する
新しいプロジェクトを作成したら、Visual Studio テンプレートを使用してユーザー コントロールを作成します。 次の手順では、プロジェクトにユーザー コントロールを追加する方法を示します。
Visual Studio で、Project Explorer ペインを見つけます。 プロジェクトを右クリックし、[ 追加>User コントロール] を選択します。
カスタム コントロール (Windows フォーム) 項目を選択します。
[名 ボックスに、ユーザー コントロールの名前を入力します。 Visual Studio には、使用できる既定の一意の名前が用意されています。 次に、を押してを追加します。
コントロールの デザイン モードで、 F7 キーを押すか、 コード ビューへの切り替えリンクを クリックします。
ヒント
ソリューション エクスプローラー ウィンドウでファイルを右クリックし、[コードの表示] を選択することもできます。
カスタム コントロールをボタンに変更する
このセクションでは、クリック回数をカウントして表示するボタンにカスタム コントロールを変更する方法について説明します。
プロジェクトに追加した後、コントロール デザイナーを開く必要があります。 そうでない場合は、ソリューション エクスプローラーのでコントロールをダブルクリックします。 カスタム コントロールを Button
から継承して拡張するコントロールに変換するには、次の手順に従います。
コントロール デザイナーを開いた状態で、F7
押すか、デザイナー ウィンドウを右クリックし、[コードの表示] 選択します。 コード エディターに、クラス定義が表示されます。
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
基底クラスを
Control
からButton
に変更します。重要
Visual Basic
使用している場合、基本クラスはコントロールの *.designer.vb ファイルで定義されます。 Visual Basic で使用する基本クラスはSystem.Windows.Forms.Button
です。_counter
という名前のクラス スコープ変数を追加します。private int _counter = 0;
Private _counter As Integer = 0
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
最後に、
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 つ増える。 クリックの合計は、ボタンの上部にテキストとして出力されます。
.NET Desktop feedback