Mouse Click Event in Timer in c# - c#

I'm having trouble with mouse click event in a timer in c#.
My timer's interval is 100 and set on True, i want to make action each 100 ticks and indentify the type of click in. I want to play the action each 100 ticks when a mouse is pressed, but this only plays one time.
EDIT: I don't want to have to enable/disable the time.
private void timer1_Tick(object sender, MouseEventArgs e)
{
if (MouseButtons == MouseButtons.Left)
{
//Action...
}
if (MouseButtons == MouseButtons.Right)
{
//Action...
}
}

100 ticks is 0.01 milliseconds and Interval is an integer so I used seconds for this test.
This webform will detect the last clicked mouse button and change the timer interval in the timer tick event based on the mouse button that was last clicked. I also added a label for visual indication:
public partial class Form1 : Form
{
Timer t;
MouseButtons lastMouseButtonClicked;
Label lblStatus;
public Form1()
{
InitializeComponent();
lblStatus = new Label()
{
Text = "No click since tick."
,Width = 500
};
this.Controls.Add(lblStatus);
t = new Timer();
//A single tick represents one hundred nanoseconds or one ten-millionth of a second. There are 10,000 ticks in a millisecond, or 10 million ticks in a second.
//t.Interval = (int)(100 / TimeSpan.TicksPerMillisecond);//0.01 ms
t.Interval = 1000;
t.Tick += T_Tick;
t.Enabled = true;
this.MouseClick += Form1_MouseClick;
}
private void T_Tick(object sender, EventArgs e)
{
switch (lastMouseButtonClicked)
{
case MouseButtons.Left:
//Action...
lblStatus.Text = "MouseButtons.Left";
t.Interval = 1000;
break;
case MouseButtons.Right:
//Action...
lblStatus.Text = "MouseButtons.Right";
t.Interval = 3000;
break;
default:
lblStatus.Text = "No click since tick.";
break;
}
LastMouseButtonClicked = MouseButtons.None;
}
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
lastMouseButtonClicked = e.Button;
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
The timer interval is changed to either 1s or 3s depending on the mouse click. You could also change the interval in the mouse click event to simplify it.
Here is an example:
public partial class Form1 : Form
{
Timer t;
MouseButtons LastMouseButtonClicked;
Label lblStatus;
DateTime previousTick;
TimeSpan elapsed;
public Form1()
{
InitializeComponent();
lblStatus = new Label()
{
Text = "No click since tick."
,
Width = 1000
};
this.Controls.Add(lblStatus);
t = new Timer();
//A single tick represents one hundred nanoseconds or one ten-millionth of a second. There are 10,000 ticks in a millisecond, or 10 million ticks in a second.
//t.Interval = (int)(100 / TimeSpan.TicksPerMillisecond);//0.01 ms
t.Interval = 1000;
t.Tick += T_Tick;
t.Enabled = true;
this.MouseClick += Form1_MouseClick;
elapsed = TimeSpan.Zero;
}
private void T_Tick(object sender, EventArgs e)
{
if (elapsed == TimeSpan.Zero)
{
elapsed += new TimeSpan(0, 0, 0, 0, 1);
}
else
{
elapsed += DateTime.Now - previousTick;
}
switch (LastMouseButtonClicked)
{
case MouseButtons.Left:
//Action...
lblStatus.Text = "MouseButtons.Left " + elapsed.Seconds;
break;
case MouseButtons.Right:
//Action...
lblStatus.Text = "MouseButtons.Right " + elapsed.Seconds;
break;
default:
lblStatus.Text = "No click since tick. " + elapsed.Seconds;
break;
}
previousTick = DateTime.Now;
}
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
switch (e.Button)
{
case MouseButtons.Left:
LastMouseButtonClicked = e.Button;
t.Interval = 1000;
break;
case MouseButtons.Right:
LastMouseButtonClicked = e.Button;
t.Interval = 3000;
break;
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}

If you are really really lazy and don't want to read the answer in my comment or redo the logic, you can perhaps modify your code like this (if this was your intent all along):
private void timer1_Tick(object sender, EventArgs e)
{
if (MouseButtons == MouseButtons.Left)
{
//Action...
}
if (MouseButtons == MouseButtons.Right)
{
//Action...
}
}
MouseButtons as in Control.MouseButtons
This is the method that is used to get the mouse button state within a timer_tick, with no need for a mouse click event, as per your request.
UPDATE Could you please mention what kind of timer are you using? They all have different behaviours and gotchas.
System.Windows.Forms.Timer, System.Timers.Timer and System.Threading.Timer.
I ask because sometimes there is an AutoReset property that you should set to true if you want more than one timer_tick to occur. I could be wrong, but this sounds like what you are describing, so it's worth a shot!
#Soenhay: I suspect that OP wanted to "loop and execute actions every t ticks" WHILE the mouse is held down. a.f.a.i.k. MouseClick triggers after MouseUp. You could modify the code to use MouseButtons (the static WinForms oddity) to check the state of the buttons.
#OP: Without you posting additional code, there is no way I see for anyone to help you further other than taking stabs in the dark with random code. Right now you have at least 3 examples similar to what you need, new knowledge about the static MouseButtons class, so I think you can and should take it from here or else you learn nothing!

Related

c# timer is going too fast if set

i'm trying to implement a simple countdown using Timer (using https://www.geoffstratton.com/cnet-countdown-timer code). it does work if i run the timer once but if i stop the timer or the timer goes to 00:00 the next time i'll start it, it will go 2x faster. if i stop it and start it again it will go 3x faster.
(my explaination may be not clear, i did a gif that demonstrate the problem)
https://media.giphy.com/media/fQr7sX6LNRECvQpCYP/giphy.gif
i'm very novice at c#, i usually figure things out but i cant get what's happening here.
I included the timer code. if somebody can help me with this it would be awesome!
Thanks !!!
private void btnStartTimer_Click(object sender, EventArgs e)
{
if (txtTimer.Text == "00:00")
{
MessageBox.Show("Please enter the time to start!", "Enter the Time", MessageBoxButtons.OK);
}
else
{
string[] totalSeconds = txtTimer.Text.Split(':');
int minutes = Convert.ToInt32(totalSeconds[0]);
int seconds = Convert.ToInt32(totalSeconds[1]);
timeLeft = (minutes * 60) + seconds;
btnStartTimer.Enabled = false;
btnCleartimer.Enabled = false;
txtTimer.ReadOnly = true;
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Start();
}
}
private void btnStopTimer_Click(object sender, EventArgs e)
{
timer1.Stop();
timeLeft = 0;
btnStartTimer.Enabled = true;
btnCleartimer.Enabled = true;
txtTimer.ReadOnly = false;
}
private void btnCleartimer_Click(object sender, EventArgs e)
{
txtTimer.Text = "00:00";
}
private void timer1_Tick(object sender, EventArgs e)
{
if (timeLeft > 0)
{
timeLeft = timeLeft - 1;
// Display time remaining as mm:ss
var timespan = TimeSpan.FromSeconds(timeLeft);
txtTimer.Text = timespan.ToString(#"mm\:ss");
// Alternate method
//int secondsLeft = timeLeft % 60;
//int minutesLeft = timeLeft / 60;
}
else
{
timer1.Stop();
SystemSounds.Exclamation.Play();
MessageBox.Show("Time's up!", "Time has elapsed", MessageBoxButtons.OK);
}
}
You need to unsubscribe from the event in your btnStopTimer_Click method:
timer1.Tick -= timer1_Tick;
You are adding the event to Count every time you start the timer. As a result, the first time you call it there is only one event, the second time two events and so on. As a result, you first go down one second, then two,....
I would recommend creating the timer separately and just call Start and Stop.
Alternativ, user Dmitry Korolev answered a good Approach if you don't want to create the timer somewhere else
timer1.Tick -= timer1_Tick;

How I can stop a timer with seconds in real time c#?

I'm trying to stop a timer when 16 seconds in real time have passed, but i don't know how i can do that.
I made this little example: when picturebox1 intersects with picturebox2,this action activate a timer, and this timer have to shows the picturebox3 during 16 seconds in real time and after stop it(timer) (and the picturebox3 doesn't show).
(Sorry for my english. But StackOverflow in Spanish doesn't have many information).
I'm using windows form and C#
private void timer2_Tick(object sender, EventArgs e)
{
pictureBox7.Hide();
if ((pictureBox3.Bounds.IntersectsWith(pictureBox2.Bounds) && pictureBox2.Visible) || (pictureBox5.Bounds.IntersectsWith(pictureBox2.Bounds) && pictureBox2.Visible))
{
puntaje++;
this.Text = "Puntaje: " + puntaje;
if (puntaje % 5 == 0)
{
timer3.Enabled=true;
//This is the part where i want set down the timer3, timer 2 is on
}
}
You can try this, on your timer tick event handler. Timespan counts the elapsed time between two dates. On this case since its 16 seconds, we count it by negative.
private void timer1_Tick(object sender, EventArgs e)
{
TimeSpan ts = dtStart.Subtract(DateTime.Now);
if (ts.TotalSeconds <= -16)
{
timer1.Stop();
}
}
Make sure your dtStart (DateTime) is declared when you start your timer:
timer1.Start();
dtStart = DateTime.Now;
The cleanest way I can see this implemented is by using the interval parameter of a System.Timers.Timer.
Here's a sample snippet of the code
var timer = new Timer(TimeSpan.FromSeconds(16).TotalMilliseconds) { AutoReset = false };
timer.Elapsed += (sender, e) =>
{
Console.WriteLine($"Finished at exactly {timer.Interval} milliseconds");
};
_timer.Start();
The TimeSpan.FromSeconds(16).TotalMilliseconds basically converts to 16000 but I used the TimeSpan static method for you to understand it easier and looks more readable.
The AutoReset property of the timer tells it that it should only be triggered once.
Adjusted for your code
private void timer2_Tick(object sender, EventArgs e)
{
pictureBox7.Hide();
if ((pictureBox3.Bounds.IntersectsWith(pictureBox2.Bounds) && pictureBox2.Visible)
|| (pictureBox5.Bounds.IntersectsWith(pictureBox2.Bounds) && pictureBox2.Visible))
{
puntaje++;
this.Text = "Puntaje: " + puntaje;
if (puntaje % 5 == 0)
{
var timer3 = new Timer(TimeSpan.FromSeconds(16).TotalMilliseconds) { AutoReset = false };
timer3.Elapsed += (sender, e) =>
{
pictureBox3.Visible = true;
};
timer3.Start();
}
}
}
Please do mark the question Answered if this solves your issue.

Simple Timer in C#

I have this code
private void picTop_MouseEnter(object sender, EventArgs e)
{
if (timer1.Tick == 10)
{
picBottom.Visible = true;
picTop.Visible = false;
timer1.Stop();
}
else
{
MessageBox.Show("ERROR You cannot view this section at this time.\nPlease try again later.");
}
}
private void picBottom_MouseEnter(object sender, EventArgs e)
{
picBottom.Visible = false;
picTop.Visible = true;
timer1.Start();
}
My timerinterval is set at 1000ms (so 1 second)
I only want the user to go into the top panel again after 10 seconds.
Some help would be greatly appreciated.
Current error I get: timer1.Tick is red underlined, error=
"The event 'System.Windows.Forms.Timer.Tick' can only appear on the left hand side of += or -="
Timer.Tick is not property its an event.
Use it like
timer1.Tick +=
{
picBottom.Visible = true;
picTop.Visible = false;
timer1.Stop();
}
For interval use timer.Interval
timer.Interval = 10000;
Ok. I think I understand what you're trying to achieve...
You have 2 areas on your form called "Top" & "Bottom"
Once the user enters & subsequently leaves the top area, you don't want them to be able to enter again for 10 seconds. is that correct?
So you've got a few problems... first of all, Tick is an event to which you would attach a method to be fired when it is raised. it's not an integer you can check. The only integer property on a timer of relevance for timing for is called Interval
But aside from that I don't think your method is going to be particularly effective.
Perhaps a better idea would be to add a MouseExit event to the top area. and disable that area for 10 seconds. and use a timer to re-enable it.
timer1.Tick += timer1_Tick;
public void Top_MouseExit (object sender, EventArgs e)
{
PicTop.Visible = false; // or hide/disbale it some other way
Timer1.Interval = 10000; //10 seconds
Timer1.Start();
}
public void timer1_Tick(object sender, EventArgs e)
{
timer1.Stop();
PicTop.Visible = true; //renable the top area
}

moving a button using a timer

I have four buttons that are called "ship1,ship2" etc.
I want them to move to the right side of the form (at the same speed and starting at the same time), and every time I click in one "ship", all the ships should stop.
I know that I need to use a timer (I have the code written that uses threading, but it gives me troubles when stopping the ships.) I don't know how to use timers.
I tried to read the timer info in MDSN but I didn't understand it.
So u can help me?
HERES the code using threading.
I don't want to use it. I need to use a TIMER! (I posted it here because it doesnt give me to post without any code
private bool flag = false;
Thread thr;
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
flag = false;
thr = new Thread(Go);
thr.Start();
}
private delegate void moveBd(Button btn);
void moveButton(Button btn)
{
int x = btn.Location.X;
int y = btn.Location.Y;
btn.Location = new Point(x + 1, y);
}
private void Go()
{
while (((ship1.Location.X + ship1.Size.Width) < this.Size.Width)&&(flag==false))
{
Invoke(new moveBd(moveButton), ship1);
Thread.Sleep(10);
}
MessageBox.Show("U LOOSE");
}
private void button1_Click(object sender, EventArgs e)
{
flag = true;
}
Have you googled Windows.Forms.Timer?
You can start a timer via:
Timer timer = new Timer();
timer.Interval = 1000; //one second
timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
timer.Enabled = true;
timer.Start();
You'll need an event handler to handle the Elapsed event which is where you'll put the code to handle moving the 'Button':
private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
MoveButton();
}

C# - Countdown Timer Using NumericUpDown as Interval

I'm sure this has been asked before, but I cannot seem to find a solution that works. I have a NumericUpDown on my form and a label along with a timer and a button. I want the timer to start when the button is pressed and the interval for the timer to equal that of the NumericUpDown and a countdown will be displayed in the label. I know this should be easy. Any help?
So far:
int tik = Convert.ToInt32(TimerInterval.Value);
if (tik >= 0)
{
TimerCount.Text = (tik--).ToString();
}
else
{
TimerCount.Text = "Out of Time";
}
It doesn't seem to update as the timer ticks.
Here is a quick example to what you are looking for. This should give you a basic idea on what you need to do
//class variable
private int totNumOfSec;
//set the event for the tick
//and the interval each second
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Interval = 1000;
private void button1_Click(object sender, EventArgs e)
{
totNumOfSec = (int)this.numericUpDown1.Value;
timer1.Start();
}
void timer1_Tick(object sender, EventArgs e)
{
//check the timer tick
totNumOfSec--;
if (totNumOfSec == 0)
{
//do capture
MessageBox.Show("Captured");
timer1.Stop();
}
else
label1.Text = "Caputring in " + totNumOfSec.ToString();
}

Categories

Resources