图片框,双击&单击事件

时间:2013-11-21 21:21:50

标签: c# events event-handling mouseevent

我有一个奇怪的问题。我有一个图片框双击事件以及单击事件。问题是我双击控件,单击事件被引发[如果我禁用单击事件,双击事件正在工作]。 This problem has been discussed here ,但没有人给出正确答案

1 个答案:

答案 0 :(得分:1)

拥有派生的Picturebox控件类

class PictureBoxCtrl:System.Windows.Forms.PictureBox
{
    // 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);
    }
}

然后使用类

使用create对象
            PictureBoxCtrl imageControl = new PictureBoxCtrl();               
            imageControl.DoubleClick += new EventHandler(picture_DoubleClick);
            imageControl.Click += new EventHandler(picture_Click);

然后根据您的要求实施picture_Click和picture_DoubleClick

void picture_Click(object sender, EventArgs e)
{
  //Custom Implementation
}

void picture_DoubleClick (object sender, EventArgs e)
{
  //Custom Implementation
}

Reference for this implementation