2013-11-21 166 views
1

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

+0

只有当所有的点击完成后,您才必须触发事件。可能是你可以使用一个计时器,然后计数来保持点击次数。如果它只触发一次单击,如果它是2,则触发双击事件 – HappyLee

+0

类似于此http://support.microsoft.com/default.aspx?kbid=109865 – RobinAtTech

+1

是的,这是正确的 – HappyLee

回答

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

然后用使用类

  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