How can I restart the Timer after 10 seconds counting down? - c#

I need to add a Timer displaying a countdown from 10. When it reaches 0 it then needs to start again from 10. This needs to be in a continuous loop.
This is what I have so far:
InitializeComponent();
timer1.Interval = 1000;
timer1.Tick += myTimer_Tick;
timer1.Start();
private void myTimer_Tick(object sender, EventArgs e)
{
label2.Text = timeLeft.ToString();
timeLeft -= 1;
if (timeLeft < 0)
{
timer1.Tick += myTimer_Tick;
}
}
What's happening now is when it reaches 0 it continues in the minus, eg. -1 -2 -3 etc.
I need it to start again from 10.
Thanks

Why not simply set the timeLeft back to ten
private void myTimer_Tick(object sender, EventArgs e)
{
label2.Text = timeLeft.ToString();
timeLeft -= 1;
if (timeLeft < 0)
{
timeLeft = 10;
}
}

Related

How do i change the value of progressbar from minutes?

i'm doing a "shutdowner" project in WinForm.
I can enter there a amount of minutes, after these minutes the pc shutdown.
Now i create a progressbar, min Value of 0, Max Value of 100.
e.g:
I enter 3 minutes (180 sec), and click on a "start" button, the value should be 0, and it should be 100 when the PC shutdowns (after the entered minutes).
I tried some math, but i didn't got it to work.
Thanks for you help!
Try something like...
private int totalSeconds;
private DateTime targetTime;
private void button1_Click(object sender, EventArgs e)
{
int mins = (int)numericUpDown1.Value;
if (mins > 0)
{
TimeSpan ts = TimeSpan.FromMinutes(mins);
targetTime = DateTime.Now.Add(ts);
totalSeconds = (int)ts.TotalSeconds;
progressBar1.Value = 0;
button1.Enabled = false;
timer1.Interval = 1000;
timer1.Start();
}
}
private void timer1_Tick(object sender, EventArgs e)
{
TimeSpan ts = targetTime.Subtract(DateTime.Now);
if (ts.TotalMilliseconds > 0)
{
label1.Text = "-" + ts.ToString(#"mm\:ss");
double percent = ((double)totalSeconds - ts.TotalSeconds) / (double)totalSeconds;
progressBar1.Value = (int)(progressBar1.Maximum * percent);
}
else
{
timer1.Stop();
button1.Enabled = true;
progressBar1.Value = progressBar1.Maximum;
// ... do something here! ...
}
}

Auto Switch between multiple forms with timer

I have 5 Forms, 1 main form, and 4 forms I want them to switch between each other Automatically every couple of seconds (take turns, each form x seconds and switch to the next).
I have 2 forms so far switching between each other every 2 seconds.
void mytimer_Tick(object sender, EventArgs e)
{
if (!frm2.Focused)
frm2.Focus();
else
frm3.Focus();
}
private void Form1_Load_1(object sender, EventArgs e)
{
Timer mytimer = new Timer();
mytimer.Tick += mytimer_Tick;
mytimer.Interval = 2000;
mytimer.Start();
}
Thankyou.
Crude format. But you will get the idea.
private void HideAllForms()
{
frm1.Hide();
frm2.Hide();
frm3.Hide();
frm4.Hide();
}
void mytimer_Tick(object sender, EventArgs e)
{
if (frmSrl == 1)
{
frmSrl++;
HideAllForms();
frm1.Show();
}
else if (frmSrl == 2)
{
frmSrl++;
HideAllForms();
frm2.Show();
}
else if (frmSrl == 3)
{
frmSrl++;
HideAllForms();
frm3.Show();
}
else if (frmSrl == 4)
{
frmSrl =1;
HideAllForms();
frm4.Show();
}
else
frmSrl = 1;
}
int frmSrl = 1;
private void Form1_Load_1(object sender, EventArgs e)
{
Timer mytimer = new Timer();
mytimer.Tick += mytimer_Tick;
mytimer.Interval = 2000;
mytimer.Start();
}

Preventing a System.Windows.Forms.Timer from accumulating Intervals?

I'm coding a Family Feud game in C# and after each answer is submitted the 30-second timer is supposed to reset. The problem is I'm accumulating 1-second Intervals so that the counter is counting down faster and faster. I can't figure out how to prevent the Intervals from accumulating. I want it to stay a fixed 1 seconds no matter how many times the button is pressed.
Code:
public System.Windows.Forms.Timer _timer = new System.Windows.Forms.Timer();
public int time = 30;
private void Check_Click(object sender, RoutedEventArgs e)
{
time = 30;
_timer.Tick += timer_Tick;
_timer.Start();
uxLabel1.Content = time.ToString();
//additional code
}
private void timer_Tick(object sender, EventArgs e)
{
_timer.Interval = 1000;
time--;
if (time == 0)
{
_timer.Stop();
strike();
}
uxLabel1.Content = time.ToString();
}
Don't put _timer.Tick += timer_Tick; in the button click code, put it once in the constructor of your form and have that be the only event registration.
public partial class YourForm : Form
{
public YourForm()
{
InitializeComponets();
_timer = new System.Windows.Forms.Timer();
_timer.Tick += timer_Tick;
_timer.Interval = 1000;
}
private System.Windows.Forms.Timer _timer;
private int time = 30;
private void Check_Click(object sender, RoutedEventArgs e)
{
time = 30;
_timer.Start();
uxLabel1.Content = time.ToString();
//additional code
}
private void timer_Tick(object sender, EventArgs e)
{
time--;
if (time == 0)
{
_timer.Stop();
strike();
}
uxLabel1.Content = time.ToString();
}
}

Disable a form's KeyDown after timer.stop()

I just created a letter game. While the timer is on it prints letters to a listbox.
The user must press the correct key. If he does, a label with the mumber of correct presses is updated. If not, a missed label is updated. In both cases, the Total and and accuracy label is updated.
The problem is that after the game is over it still counts.
The thing I want to do is disable keyDown event from happening on the form.
I wrote this code but doesn't work.
timer1.Stop();
Form1 form = new Form1();
form.KeyPreview = false;
Does anyone has a solution?
This is my code. Even if I add a flag, the form, even after the game is over, has keyDown Event enabled. I want to know if there is a way to disable the keyDown event from happening after the game finishes.
namespace TypingGame
{
public partial class Form1 : Form
{
Random random = new Random();
Stats stats = new Stats();
public Form1()
{
InitializeComponent();
}
//Here the game starts
private void timer1_Tick(object sender, EventArgs e)
{
listBox1.Items.Add((Keys) random.Next(65,90));
//If letters missed are more than 7 the game is over
//so I need a way to disable the KeyDown event from happening after the game is over
if (listBox1.Items.Count > 7)
{
listBox1.Items.Clear();
listBox1.Items.Add("Game Over");
timer1.Stop();
}
}
//Here is what happens on KeyDown.
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (listBox1.Items.Contains(e.KeyCode))
{
listBox1.Items.Remove(e.KeyCode);
listBox1.Refresh();
if (timer1.Interval > 400)
{
timer1.Interval -= 10;
}
if (timer1.Interval > 250)
{
timer1.Interval -= 7;
}
if (timer1.Interval > 100)
{
timer1.Interval -= 2;
}
difficultyProgressBar.Value = 800 - timer1.Interval;
stats.Update(true);
}
else
{
stats.Update(false);
}
correctLabel.Text = "Correct: " + stats.Correct;
missedLabel.Text = "Missed: " + stats.Missed;
totalLabel.Text = "Total: " + stats.Total;
accuracyLabel.Text = "Accuracy" + stats.Accuracy + "%";
}
}
}
I have updated the code base on your code.
MSDN Reference - Timer
//Here the game starts (?? game stop?)
private void timer1_Tick(object sender, EventArgs e)
{
listBox1.Items.Add((Keys) random.Next(65,90));
//If letters missed are more than 7 the game is over
//so I need a way to disable the KeyDown event from happening after the game is over
if (listBox1.Items.Count > 7)
{
listBox1.Items.Clear();
listBox1.Items.Add("Game Over");
// timer1.Stop();
// disable the timer to stop
timer1.Enabled = false;
}
}
//Here is what happens on KeyDown.
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
// if timer disabled, do nothing
if (!timer1.Enabled) return;
if (listBox1.Items.Contains(e.KeyCode))
{
listBox1.Items.Remove(e.KeyCode);
listBox1.Refresh();
if (timer1.Interval > 400)
{
timer1.Interval -= 10;
}
if (timer1.Interval > 250)
{
timer1.Interval -= 7;
}
if (timer1.Interval > 100)
{
timer1.Interval -= 2;
}
difficultyProgressBar.Value = 800 - timer1.Interval;
stats.Update(true);
}
else
{
stats.Update(false);
}
correctLabel.Text = "Correct: " + stats.Correct;
missedLabel.Text = "Missed: " + stats.Missed;
totalLabel.Text = "Total: " + stats.Total;
accuracyLabel.Text = "Accuracy" + stats.Accuracy + "%";
}

Window form opacity .. How to control?

I have a single form window application now I want to change the form opacity when application runs. Means when application run it will show low opacity form and as time increse it will show complete form with 100 opacity. So how to do that. (should I use timer control to control opacity, if yes then how????)
in constructor of the form you can write something like this.
this.Opacity = .1;
timer.Interval = new TimeSpan(0, 0, intervalinminutes);
timer.Tick += ChangeOpacity;
timer.Start();
And then define a method like this
void ChangeOpacity(object sender, EventArgs e)
{
this.Opacity += .10; //replace.10 with whatever you want
if(this.Opacity == 1)
timer.Stop();
}
To fade forms in and out, I usually do this:
for(double opacity = 0.0; opacity <= 1.0; opacity += 0.2) {
DateTime start = DateTime.Now;
this.Opacity = opacity;
while(DateTime.Now.Subtract(start).TotalMilliseconds <= 30.0) {
Application.DoEvents();
}
}
It's a nice, simple solution if you'll be doing it very infrequently. Otherwise, I would recommend using threads.
In the constructor, start the timer control that will call a method at each tick.
timer.Interval = 1000;
timer.Tick += new EventHandler(TimerEventProcessor);
timer.Start();
............
private static void TimerEventProcessor(Object myObject,
EventArgs myEventArgs)
{
if(this.Opacity < 1)
this.Opacity += .1;
else
timer.Stop();
}
For Exit Decreasing Opacity Animation
///////\\\\\\ Coded by Error X Tech ///////\\\\\\
System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
private void DecreaseOpacity(object sender, EventArgs e)
{
if (this.Opacity >= 0.1)
{
this.Opacity -= 0.04; //replace 0.04 with whatever you want
}
if (this.Opacity <= 0.0)
timer.Stop();
if (this.Opacity <= 0.1)
{
System.Environment.Exit(1);
Process.GetCurrentProcess().Kill();
}
}
private void Exit_Click(object sender, EventArgs e)
{
timer.Interval = 47; //replace 47 with whatever you want
timer.Tick += DecreaseOpacity;
timer.Start();
}
In the constructor, set the opacity to 0 and start a timer with an interval of something small like 10 or 100 milliseconds. In the timer_Tick event, you simply need to run this.Opacity += 0.01;
This will make it so that the opacity starts at 0 and increase by .01 every few milliseconds until it's 1 (opacity is a double, when it reaches a value of 1 it's fully opaque)
increasing Opacity Animation while Application start
///////\\\\\\ Coded by Error X Tech ///////\\\\\\
System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
void IncreaseOpacity(object sender, EventArgs e)
{
if (this.Opacity <= 1) //replace 0.88 with whatever you want
{
this.Opacity += 0.01; //replace 0.01 with whatever you want
}
if (this.Opacity == 1) //replace 0.88 with whatever you want
timer.Stop();
}
private void Form1_Load(object sender, EventArgs e)
{
this.Opacity = .01;
timer.Interval = 10; //replace 10 with whatever you want
timer.Tick += IncreaseOpacity;
timer.Start();
}

Categories