Picture box, Double click & Single click events

戏子无情 提交于 2020-01-05 14:59:10

问题


I have an strange issue. I have a picture box double click event as well as single click event. The problem is even I double click the control, single click event is raised [If I disable single click event, double click event is working]. This problem has been discussed here , but nobody gave a right answer


回答1:


Have a derived Picturebox control class

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

Then use create objects by using the class

            PictureBoxCtrl imageControl = new PictureBoxCtrl();               
            imageControl.DoubleClick += new EventHandler(picture_DoubleClick);
            imageControl.Click += new EventHandler(picture_Click);

Then Implement picture_Click and picture_DoubleClick with your requirements

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

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

Reference for this implementation



来源:https://stackoverflow.com/questions/20132136/picture-box-double-click-single-click-events

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!