1
votes

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

2
Does this answer your question? how to avoid click when I want double click?Dash
Why don't you move these two distinct functionalities to two distinct selectors of the ContextMenu of your NotifyIcon?Jimi
@Jimi i can't use context menu since the functionality are all determined by othersmyelxx
Suggestion: handle the MouseClick event only in combination with the Control.ModifierKeys. Open the folder if (ModifierKeys == Keys.Control), otherwise show the form.dr.null

2 Answers

3
votes

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

2
votes

i figured out on how to handle this is to add task delay on my mouse click event to wait if it triggers the double click event at the same time.

thank you for the answers!

 private bool isSingleClicked;

 private void notifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
    {
        isSingleClicked = false;
        //double click process
    }
 private async void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
    {
       //delay click event to check if it trigger double click event
        isSingleClicked = true;
        await Task.Delay(100);

        //check if it trigger double click event, will not proceed to the next line
        if (!isSingleClicked) return; 

        //process of mouse click
        isSingleClicked = false;
    }