Share via


Comment : créer un événement double-clic personnalisé

Mise à jour : novembre 2007

Le .NET Compact Framework ne prend pas en charge l'événement DoubleClick Windows Forms pour un bouton. Toutefois, vous pouvez créer un contrôle qui dérive de la classe Button pour implémenter l'événement.

Pour créer un événement double-clic personnalisé

  1. Créez une classe qui dérive de la classe System.Windows.Forms.Button.

  2. Déclarez un événement DoubleClick.

  3. Substituez la méthode OnClick par du code pour déclencher l'événement DoubleClick si les clics de bouton se produisent au cours d'une période spécifiée.

Exemple

Cet exemple crée un contrôle personnalisé DoubleClickButton et l'implémente sur un formulaire.

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

namespace ButtonDClick
{
    public class 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);
        }

        protected override void Dispose( bool disposing )
        {
            base.Dispose( disposing );
        }

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

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

        static void Main() 
        {
            Application.Run(new Form1());
        }

        // Derive a button with extended funtionality
        // from the Button class.
        public class 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;

            public new event EventHandler DoubleClick;

            protected override void 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.
            protected new virtual void OnDoubleClick(EventArgs e)
            {
                if (this.DoubleClick != null)
                    this.DoubleClick(this, e);
            }
        }
    }
}

Compilation du code

Cet exemple nécessite des références aux espaces de noms suivants :

Voir aussi

Concepts

Développement de contrôle personnalisé