I am new C# and I am using windows forms. I built an application where I want to show a massage if the mouse stops (no activities are going) for 5 seconds using a timer and when the mouse moves the timer gets reset.
I have Form1
with some controls (Buttons
and TextBoxes
) and a Timer
. I am just trying to do something like a screensaver, so when Form1
loads and there are no activities for a certain time (mouse stops) an action has to be taken (for example show message).
I tried the following code (as an example) but it doesn't work well, when Form1
loads the timers start counting and if I move mouse (before i == 5
) the timer resets and it never starts counting again.
int i = 0;
private void Form1_Load(object sender, EventArgs e)
{
timer1.Start();
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
i = 0;
timer1.Stop();
textBox1.Text = i.ToString();
}
private void Form1_MouseHover(object sender, EventArgs e)
{
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
i = i + 1;
textBox1.Text = i.ToString();
if(i==5)
{
MessageBox.Show("Time is over");
}
}
I do not know if I am using the right mouse events and I do not even know if it is correct to use those events in such a situation. How can I show a message if the mouse doesn't move for 5 seconds?