This code I wrote for wpf
also works with a slight change in winforms
.
DispatcherTimer timer;
int clickCount = 0;
public MainWindow()
{
InitializeComponent();
timer = new DispatcherTimer();
timer.Tick += Timer_Tick;
timer.Interval = TimeSpan.FromMilliseconds(200);
}
private void MyButton_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
clickCount++;
timer.Start();
}
private void Timer_Tick(object sender, EventArgs e)
{
timer.Stop();
if (clickCount >= 2)
{
MessageBox.Show("doubleclick");
}
else
{
MessageBox.Show("click");
}
clickCount = 0;
}
for winforms:
System.Timers.Timer timer;
int clickCount = 0;
public Form1()
{
InitializeComponent();
timer = new System.Timers.Timer();
timer.Elapsed += Timer_Elapsed;
//SystemInformation.DoubleClickTime default is 500 Milliseconds
timer.Interval = SystemInformation.DoubleClickTime;
//or
timer.Interval = 200;
}
private void myButton_MouseDown(object sender, MouseEventArgs e)
{
clickCount++;
timer.Start();
}
private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
timer.Stop();
if (clickCount >= 2)
{
MessageBox.Show("doubleclick");
}
else
{
MessageBox.Show("click");
}
clickCount = 0;
}
With the code I mentioned in the wpf
example, you can change SystemInformation.DoubleClickTime
to your desired time
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…