Click here to Skip to main content
15,905,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a C# winForms code that when mouse hovers over a button then it immediately triggers a click event of a button..
but i want that if curosor hovers a button for 3 seconds then then it should perform click event.
I have tried a timer but i am unable to stop a timer nd it calls timer_tick randomly nd continuously...



Edited code..

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication23
{
    public partial class Form1 : Form
    {


        public Form1()
        {
            InitializeComponent();

            //this.MouseHover += new System.EventHandler(this_MouseHover);
            button1.MouseHover += new EventHandler(button1_MouseHover);
            button1.MouseLeave += new EventHandler(button1_MouseLeave);

        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("button clicked after 3 seconds",counter.ToString());
        }

        private void button1_MouseHover(object sender, EventArgs e)
        {
            label1.Text = "Mouse hover";

            counter =0;
            //timer1.Interval = 1000 - SystemInformation.MouseHoverTime;
            //Timer timer1 = new Timer();
            timer1.Tick += new EventHandler(timer1_Tick_1); // Everytime timer ticks, timer_Tick will be called
            timer1.Interval = 1000;
            timer1.Start();
        }




        int counter;


        private void button1_MouseLeave(object sender, EventArgs e)
        {
            timer1.Stop();
            //MessageBox.Show("leave",counter.ToString());
        }

        private void timer1_Tick_1(object sender, EventArgs e)
        {
            counter++; button1.Text = counter.ToString();
            if (counter==3)
            {
                timer1.Stop();


                button1.PerformClick();

            }
        }




    }
}
Posted
Updated 29-Aug-13 9:56am
v3
Comments
[no name] 28-Aug-13 15:01pm    
Okay so why are you unable to stop a timer when the rest of the world is able to?
Member 9129971 29-Aug-13 14:41pm    
console app for timer is working properly bt i dont know why its not working on winForms.I have posted a code
[no name] 29-Aug-13 14:53pm    
Most if not all of your problem probably stems from the fact that every time that you "hover" over the button, you are creating a new timer. Try creating one timer once and see if that makes any difference.
Member 9129971 29-Aug-13 15:12pm    
tried it bt same problem arises..that since i have stopped the timer by timer.stop() bt the timer_tick is still called ...timer is not stopped when counter==3 nd still continues????
[no name] 29-Aug-13 15:16pm    
You tried what exactly? My crystal ball is in the shop this week so I cannot see your code. And I have used up all of my mind reading credits.

imho, answers to date have missed a fundamental issue: the critical Event to use to discriminate a 3-second interval where the Mouse is over a Control is MouseEnter, not MouseHover.

1. at design-time:

a. put a Button Control on the Form, 'button1: wire up its MouseEnter, and MouseLeave EventHandlers as shown below.

b. put a TextBox Control on the Form, 'textBox1: set its MultiLine property to 'true. Set its ScrollBar property to 'Vertical.

c. put a Timer Component on the Form, 'timer1: wire up its Tick EventHandler as shown below.

2. The code:
C#
private bool overTheButton;

private bool hoverOverTheButtonEventTriggered = false;

private void button1_MouseEnter(object sender, EventArgs e)
{
    overTheButton = true;
    hoverOverTheButtonEventTriggered = false;
    timer1.Start();
}

private void button1_MouseLeave(object sender, EventArgs e)
{
    overTheButton = false;
    hoverOverTheButtonEventTriggered = false;
    timer1.Stop();
}

private void button1_Click(object sender, EventArgs e)
{
    if (hoverOverTheButtonEventTriggered)
    {
        textBox1.Text += "3 second hover event triggered" + Environment.NewLine;
        hoverOverTheButtonEventTriggered = false;
    }
    else
    {
        textBox1.Text += "Regular click" + Environment.NewLine;
        timer1.Stop();
    }
}

private void timer1_Tick(object sender, EventArgs e)
{
    timer1.Stop();

    if (overTheButton && ! hoverOverTheButtonEventTriggered)
    {
        hoverOverTheButtonEventTriggered = true;
        button1.PerformClick();
    }
}
3. Run the code: observe what happens if you click the Button before the Mouse has been over the Button 3 seconds. Observe what happens if you click the Button before three-seconds of having the Mouse over it, and don't move the Mouse (it remains over the Button). Observe what happens if you move the Mouse over the Button for three seconds and leave the Mouse over the Button after the first 3-second Click occurs ... and so forth.

4. Note the assumptions made here:

a. the programmer wants to do one thing if the Button gets a Click because the Timer elapsed, and, possibly, another, if the Button is clicked before the Timer elapses.

b. in the case the user leaves the Mouse over the Button, and a Timer-elapsed Click event is triggered: the programmer does not want to start the Timer again just because the Mouse remains over the Button.

c. in the case the user clicks on the Button before three-seconds of having the Mouse over it: the programmer doesn't want a Timer based event to be triggered just because the Mouse stays over the Button.

~

If your use-case doesn't make the same assumptions: please, change this code :)
 
Share this answer
 
v2
Comments
Member 9129971 30-Aug-13 16:05pm    
thanks for ur contribution..yes u are right bt..i want 2,3 or 4 sec delay not exact so thats why i hv no issue if i use mousehovr or mouseEnter..by using ur help nd others im almost done with my code bt only one issue remanning..
1)timer_tick_1 is called twice ..why?

following code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication23
{
public partial class Form1 : Form
{

int counter=0;

public Form1()
{
InitializeComponent();

//this.MouseHover += new System.EventHandler(this_MouseHover);
button1.MouseEnter += new EventHandler(button1_MouseHover);
button1.MouseLeave += new EventHandler(button1_MouseLeave);
timer1.Tick += new EventHandler(timer1_Tick_1);

}

private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("button clicked after 3 seconds",counter.ToString());
}

private void button1_MouseHover(object sender, EventArgs e)
{
label1.Text = "Mouse hover";


if (timer1.Enabled==false)
{
//_isTimerRunning = true;
timer1.Enabled = true;
counter = 0;
timer1.Interval = 3000;
//timer1.Interval = 3000 - SystemInformation.MouseHoverTime;
timer1.Start();

}

}



private void button1_MouseLeave(object sender, EventArgs e)
{
timer1.Stop();
counter = 0;
//_isTimerRunning = false;
timer1.Enabled = false;
label1.Text = "Mouse leave";
button1.Text = counter.ToString();

}

private void timer1_Tick_1(object sender, EventArgs e)
{
counter++;
button1.Text = counter.ToString();
//if(timer1.Interval==3000)
MessageBox.Show("tick");

//if (counter>=0)
//{
timer1.Stop();
timer1.Enabled = false;
button1.PerformClick();
//counter = 0;
MessageBox.Show("if");
//}
}




}
}
OK, I deleted my last solution posted as it was not quite right and I identified another issue with your solution. The below code worked for me. The changes I made are, added the _isTimerRunning variable to prevent the timer from being repeatedly started. The second thing changed, I moved the line where you attach the event to the timer into the constructor. You were attaching a new event handler every time the mouse would hover the button. Putting it in the form's constructor will only attach the even once.
C#
timer1.Tick += new EventHandler(timer1_Tick_1);

Here is the code that worked for me...
C#
public partial class Form1 : Form
    {
        bool _isTimerRunning = false;
        int counter = 0;

        public Form1()
        {
            InitializeComponent();

            button1.MouseHover += new EventHandler(button1_MouseHover);
            button1.MouseLeave += new EventHandler(button1_MouseLeave);

            timer1.Tick += new EventHandler(timer1_Tick_1);
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("button clicked after 3 seconds",counter.ToString());
        }
 
        private void button1_MouseHover(object sender, EventArgs e)
        {
            label1.Text = "Mouse hover";
            if (!_isTimerRunning)
            {
                _isTimerRunning = true;                
                counter = 0;                
                timer1.Interval = 1000;
                timer1.Start();
            }
        }        
 
        private void button1_MouseLeave(object sender, EventArgs e)
        {
            timer1.Stop();
            counter = 0;
            _isTimerRunning = false;
            label1.Text = "Mouse leave";
            button1.Text = counter.ToString();
        }
 
        private void timer1_Tick_1(object sender, EventArgs e)
        {
             counter++; 
            button1.Text = counter.ToString();
            if (counter==3)
            {
                timer1.Stop();
                _isTimerRunning = false;
                counter = 0;
                button1.PerformClick(); 
            }
        }
    }


Also, your counter starts at 0 and goes to 3 which is going to actually be 4 seconds not 3.
 
Share this answer
 
v4
Comments
Philippe Mori 29-Aug-13 19:17pm    
You don't need an extra variable as you can check Enabled property and by the way, it is not necessary to have a counter as it is possible to direclty specify 3000ms.
idenizeni 29-Aug-13 20:35pm    
Thanks for the Enabled information. As for the counter, I based my code on the code that was posted which contained the counter.
Member 9129971 30-Aug-13 15:56pm    
yes by moving tick event to form() works fine nd I am almost done with my code nd thanks for ur contribution..bt have only one issue remainnning now
1)the timer_tick_1 is called twice..why? following is my final code



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication23
{
public partial class Form1 : Form
{

int counter=0;

public Form1()
{
InitializeComponent();

//this.MouseHover += new System.EventHandler(this_MouseHover);
button1.MouseEnter += new EventHandler(button1_MouseHover);
button1.MouseLeave += new EventHandler(button1_MouseLeave);
timer1.Tick += new EventHandler(timer1_Tick_1);

}

private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("button clicked after 3 seconds",counter.ToString());
}

private void button1_MouseHover(object sender, EventArgs e)
{
label1.Text = "Mouse hover";


if (timer1.Enabled==false)
{
//_isTimerRunning = true;
timer1.Enabled = true;
counter = 0;
timer1.Interval = 3000;
//timer1.Interval = 3000 - SystemInformation.MouseHoverTime;
timer1.Start();

}

}



private void button1_MouseLeave(object sender, EventArgs e)
{
timer1.Stop();
counter = 0;
//_isTimerRunning = false;
timer1.Enabled = false;
label1.Text = "Mouse leave";
button1.Text = counter.ToString();

}

private void timer1_Tick_1(object sender, EventArgs e)
{
counter++;
button1.Text = counter.ToString();
//if(timer1.Interval==3000)
MessageBox.Show("tick");

//if (counter>=0)
//{
timer1.Stop();
timer1.Enabled = false;
button1.PerformClick();
//counter = 0;
MessageBox.Show("if");
//}
}




}
}
idenizeni 30-Aug-13 16:46pm    
Looks like you already go your solution.
Member 9129971 30-Aug-13 17:00pm    
yes im almost done bt timer_tick_1 must be called once because i have stopped the timer ..bt there is a problem that timer_tick_1 is called twice?
 
Share this answer
 
Comments
Member 9129971 29-Aug-13 14:40pm    
checked bt cannot solve my problem..timer.stop(); is not working nd also timer.enable=false; not working properly?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900