방법: 사용자 지정 두 번 클릭 이벤트 만들기

업데이트: 2007년 11월

.NET Compact Framework에서는 단추에 대해 Windows Forms DoubleClick 이벤트를 지원하지 않습니다. 그러나 Button 클래스에서 파생되는 컨트롤을 만들어 이 이벤트를 구현할 수 있습니다.

사용자 지정 두 번 클릭 이벤트를 만들려면

  1. System.Windows.Forms.Button 클래스에서 파생되는 클래스를 만듭니다.

  2. DoubleClick 이벤트를 선언합니다.

  3. 지정한 시간 내에 단추를 여러 번 클릭할 경우 DoubleClick 이벤트를 발생시키는 코드를 작성하여 OnClick 메서드를 재정의합니다.

예제

이 예제에서는 DoubleClickButton 사용자 지정 컨트롤을 만들고 폼에서 이 컨트롤을 구현합니다.

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);
            }
        }
    }
}

코드 컴파일

이 예제에는 다음과 같은 네임스페이스에 대한 참조가 필요합니다.

참고 항목

개념

사용자 지정 컨트롤 개발