Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
856 views
in Technique[技术] by (71.8m points)

mouse click event fires before mouse double click event on notifyicon winforms c#

Using my notify icon my function for mouse click and double click event are entirely different Mouse click event - will open a form Doouble click event - will open a folder

Mouse click event do its job but when fire the double click event, it still trigger the mouse click event by which my form still show on double click.

Actual result on double click: opens the folder and opens the form Expected result on double click: opens the folder

I already close the form on click but still show for a split second before hiding since the mouse click event is fired first before double click event.

  private void notifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            //opens folder function

            //close widget on double click
            if (widgetForm != null)
                widgetForm.Close();
        }

question from:https://stackoverflow.com/questions/65912090/mouse-click-event-fires-before-mouse-double-click-event-on-notifyicon-winforms-c

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...