方法 : ユーザー設定のダブルクリック イベントを作成します。

[このドキュメントはプレビュー版であり、後のリリースで変更されることがあります。 空白のトピックは、プレースホルダーとして挿入されています。]

.NET Compact Framework は、Windows フォーム DoubleClick イベント ボタンのサポートしません。 イベントを実装するには、Button クラスから派生するコントロールを作成することができます。

カスタムのダブルクリック イベントを作成するには

  1. System.Windows.Forms.Button クラスから派生するクラスを作成します。

  2. DoubleClick イベントを宣言します。

  3. ボタンのクリックが指定された時間内で発生した場合に、OnClick イベントを発生させるコードで DoubleClick メソッドをオーバーライドします。

使用例

次の使用例は、DoubleClickButton カスタム コントロール作成し、フォームの実装します。

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

namespace ButtonDClick
{
    publicclass Form1 : System.Windows.Forms.Form
    {
        // Track the number of // double-clicks with the count variable.int count = 0;

        public Form1()
        {
            InitializeComponent();

            // Display OK button for closing.this.MinimizeBox = false;

            // Create an instance of the DoubleClickButton class.
            DoubleClickButton dClickB = new DoubleClickButton();

            dClickB.Bounds = new Rectangle(10,10,200,30);
            dClickB.Text = "Double-click me!";
            Controls.Add(dClickB);

            // Add the DClick event hander to the DoubleClick event.
            dClickB.DoubleClick += new EventHandler(DClick);
        }

        protectedoverridevoid Dispose( bool disposing )
        {
            base.Dispose( disposing );
        }

        privatevoid InitializeComponent()
        {
            this.Text = "Form1";
        }

        privatevoid DClick(object o, EventArgs e)
        {
            // Display the number of double-clicks.
            MessageBox.Show("Double-click count = " + ++count);
        }

        staticvoid Main() 
        {
            Application.Run(new Form1());
        }

        // Derive a button with extended funtionality// from the Button class.publicclass DoubleClickButton : System.Windows.Forms.Button 
        { 
            // Note that the DoubleClickTime property gets // the maximum number of milliseconds allowed between // mouse clicks for a double-click to be valid.int previousClick = SystemInformation.DoubleClickTime;

            publicnewevent EventHandler DoubleClick;

            protectedoverridevoid OnClick(EventArgs e)
            {
                int now = System.Environment.TickCount;

                // A double-click is detected if the the time elapsed// since the last click is within DoubleClickTime.if ( now - previousClick <= SystemInformation.DoubleClickTime)
                {
                    // Raise the DoubleClick event.if (DoubleClick != null)
                        DoubleClick(this,EventArgs.Empty);
                }

                // Set previousClick to now so that // subsequent double-clicks can be detected.
                previousClick = now;

                // Allow the base class to raise the regular Click event.base.OnClick(e);
            }

            // Event handling code for the DoubleClick event.protectednewvirtualvoid OnDoubleClick(EventArgs e)
            {
                if (this.DoubleClick != null)
                    this.DoubleClick(this, e);
            }
        }
    }
}

コードのコンパイル方法

この例では、次の名前空間への参照が必要です。

参照

概念

カスタム コントロールの開発