图片盒,双击&单击事件 [英] Picture box, Double click & Single click events

查看:98
本文介绍了图片盒,双击&单击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个奇怪的问题。我有一个图片框双击事件以及单击事件。问题是甚至我双击控件,单击事件被提出[如果我禁用单击事件,双击事件正在运行]。 此问题已在此讨论,但没有人给出正确答案

解决方案

有一个派生Picturebox控件类

  class PictureBoxCtrl:System.Windows.Forms.PictureBox 
{
//请注意,DoubleClickTime属性获取
//
之间允许的最大毫秒数//鼠标点击双击以使其有效。
int previousClick = SystemInformation.DoubleClickTime;
公共新事件EventHandler DoubleClick;

protected override void OnClick(EventArgs e)
{
int now = System.Environment.TickCount;
//如果最近一次点击之后的时间是
//,则会检测到双击。
if(now - previousClick <= SystemInformation.DoubleClickTime)
{
//提高DoubleClick事件。
如果(DoubleClick!= null)
DoubleClick(此为EventArgs.Empty);
}
//将previousClick设置为现在,以便可以检测到
//后续的双击。
previousClick = now;
//允许基类提高常规的Click事件。
base.OnClick(e);
}

// DoubleClick事件的事件处理代码。
protected new virtual void OnDoubleClick(EventArgs e)
{
if(this.DoubleClick!= null)
this.DoubleClick(this,e);
}
}

然后使用类

  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)
{
//自定义实现
}

void picture_DoubleClick(object发件人,EventArgs e)
{
//自定义实现
}

此实施的参考


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

解决方案

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

这篇关于图片盒,双击&amp;单击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆