我有一个奇怪的问题。我有一个图片框双击事件和单击事件。问题是,即使我双击控件,也会引发单击事件[如果我禁用单击事件,则双击事件有效]。这个问题已经在这里讨论过了,但没有人给出正确的答案
拥有派生的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
}
此实现的参考