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! ...
}
}
Related
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;
}
}
In a Winforms application, there are 3 different numericupdown such as min, sec, millisecond. How do I make a timer that counts down the value of entering numericupdowns? I have tried with if else blocks. I also saw a lot of time timespawn titles on the internet. Which is better for this countdown? if else blocks or timespawn
numericUpDownMiliSn.Value--;
if (numericUpDownMiliSn.Value == 0)
{
if (numericUpDownMiliSn.Value == 0 && numericUpDownSn.Value == 0 && numericUpDownDk.Value == 0)
{
timer2.Stop();
button2.Text = "Baslat";
durum = false;
}
else
{
if (numericUpDownSn.Value > 0)
{
numericUpDownSn.Value--;
numericUpDownMiliSn.Value = 60;
}
else
{
numericUpDownMiliSn.Value = 60;
}
if (numericUpDownSn.Value > 0)
{
numericUpDownSn.Value--;
numericUpDownSn.Value = 60;
}
}
}
From my comments in the original question above:
Timers in WinForms are NOT accurate, so you shouldn't be basing your
time off incrementing/decrementing those in a Tick() event. You should
definitely be using a TimeSpan (derived from subtracting the current
time from some future target time; based on the initial values in your
NumericUpDowns)...then simply update the NumericUpDowns with the
numbers in the TimeSpan.
Here's how that code might look:
private DateTime targetDT;
private void button1_Click(object sender, EventArgs e)
{
TimeSpan ts = new TimeSpan(0, 0, (int)numericUpDownMn.Value, (int)numericUpDownSn.Value, (int)numericUpDownMiliSn.Value);
if (ts.TotalMilliseconds > 0)
{
button1.Enabled = false;
numericUpDownMn.Enabled = false;
numericUpDownSn.Enabled = false;
numericUpDownMiliSn.Enabled = false;
targetDT = DateTime.Now.Add(ts);
timer1.Start();
}
}
private void timer1_Tick(object sender, EventArgs e)
{
TimeSpan ts = targetDT.Subtract(DateTime.Now);
if (ts.TotalMilliseconds > 0)
{
numericUpDownMn.Value = ts.Minutes;
numericUpDownSn.Value = ts.Seconds;
numericUpDownMiliSn.Value = ts.Milliseconds;
}
else
{
timer1.Stop();
numericUpDownMn.Value = 0;
numericUpDownSn.Value = 0;
numericUpDownMiliSn.Value = 0;
button1.Enabled = true;
numericUpDownMn.Enabled = true;
numericUpDownSn.Enabled = true;
numericUpDownMiliSn.Enabled = true;
}
}
What code should I write to achieve this: IDLE Timer that will start after 15 minutes of no movement in the computer and the IDLE timer will stop if you do movement in the computer again.
Additionally, I want a timer (Activity timer) to stop when the IDLE timer starts. Then the Activity timer resumes when there is movement in the pc again and the IDLE timer stops.
This is what I have done so far:
namespace TITOMS_LOGIN
{
public partial class Form3Admin: Form
{
int seconds;
int minutes;
int hours;
public Form3Admin()
{
InitializeComponent();
seconds = minutes = hours = 0;
}
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
Form2Admin sw = new Form2Admin();
sw.Show();
}
private void button3_Click(object sender, EventArgs e)
{
this.Hide();
Form5 se = new Form5();
se.Show();
}
private void Form3Admin_Load(object sender, EventArgs e)
{
Time.Text = DateTime.Now.ToShortTimeString();
Day.Text = DateTime.Now.ToLongDateString();
}
private void timer1_Tick(object sender, EventArgs e)
{
seconds++;
if (seconds > 59)
{
minutes++;
seconds = 0;
}
if (minutes > 58)
{
hours++;
minutes = 0;
}
lblhours.Text = hours.ToString() + "HRS";
lblminutes.Text = minutes.ToString() + "MINS";
}
private void button4_Click(object sender, EventArgs e)
{
timer1.Start();
}
private void button5_Click(object sender, EventArgs e)
{
timer1.Stop();
}
}
}
private void timer3_Tick(object sender, EventArgs e)
{
TimeSpan idleTime = TimeSpan.FromMinutes(0.5);
TimeSpan aa = TimeSpan.FromSeconds(1);
if (UserInput.IdleTime >= idleTime && timer1.Enabled)
{
timer2.Start();
timer1.Stop();
Console.WriteLine("Stopped Timer 1, Start Timer 2 ");
}
else if (UserInput.IdleTime < aa && timer2.Enabled)
{
timer1.Start();
timer2.Stop();
Console.WriteLine("Stopped Timer 2, Start Timer 1 ");
}
Console.WriteLine("Idle for " + UserInput.IdleTime.ToString());
}
I need to display the elapsed time dynamically. My code will pop up a message based on an interval value.
public void button1_Click(object sender, EventArgs e)
{
this.TopMost = true;
DialogResult result1 = MessageBox.Show("Add some notes to your current ticket?",
"Add Notes",
MessageBoxButtons.YesNo);
if (result1 == DialogResult.Yes)
{
Timer tm;
tm = new Timer();
int minutes = int.Parse(textBox2.Text);
tm.Interval = (int)TimeSpan.FromMinutes(minutes).TotalMilliseconds;
tm.Tick += new EventHandler(button1_Click);
tm.Enabled = true;
string pastebuffer = DateTime.Now.ToString();
pastebuffer = "### Edited on " + pastebuffer + " by " + txtUsername.Text + " ###";
Clipboard.SetText(pastebuffer);
tm.Start();
}
else if (result1 == DialogResult.No)
{
}
this.TopMost = false;
}
If I have defined 15 mins in my interval how do i get the countdown to show in a label?
You should store end-time in a filed at form level and then in Tick event handler of the timer check the difference between the end-time and now and update a label which you want to show count-down timer:
private DateTime endTime;
System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
private void button1_Click(object sender, EventArgs e)
{
var minutes = 0;
if (int.TryParse(textBox1.Text, out minutes) && timer.Enabled == false)
{
endTime = DateTime.Now.AddMinutes(minutes);
timer.Interval = 1000;
timer.Tick -= new EventHandler(timer_Tick);
timer.Tick += new EventHandler(timer_Tick);
timer.Start();
UpdateText();
}
}
void timer_Tick(object sender, EventArgs e)
{
UpdateText();
}
void UpdateText()
{
var diff = endTime.Subtract(DateTime.Now);
if (diff.TotalSeconds > 0)
label1.Text = string.Format("{0:D2}:{1:D2}:{2:D2}",
diff.Hours, diff.Minutes, diff.Seconds);
else
{
this.Text = "00:00:00";
timer.Enabled = false;
}
}
I wouldn't muck about with timers. I'd use Microsoft's Reactive Framework for this. Just NuGet "Rx-Winforms" to get the bits. Then you can do this:
Observable
.Create<string>(o =>
{
var now = DateTimeOffset.Now;
var end = now.AddMinutes(15.0);
return
Observable
.Interval(TimeSpan.FromSeconds(0.1))
.TakeUntil(end)
.Select(x => end.Subtract(DateTimeOffset.Now).ToString(#"mm\:ss"))
.DistinctUntilChanged()
.Subscribe(o);
})
.ObserveOn(this)
.Subscribe(x => label1.Text = x);
This will automatically create a countdown timer that will update the text in label1 with the following values:
14:59
14:58
14:57
14:56
14:55
14:54
...
00:02
00:01
00:00
If you want to stop this before the timer runs out the Subscribe method returns an IDisposable that you can just call .Dispose().
You should try to count the 15 minutes.
For example, if your using a Label (Label1) you should count it with a timer.
Just use a timer and count every tick (1000 milliseconds) +1
Timer1 has a +1 (declare a int as 0)
If the label reaches the number of seconds or minutes
(You can modify that with milliseconds), it stops the timer.
How can i make that if i click on the button the flag will be true and in the timer1 tick event it will change the count and will count up. And if i click the same button again it will count down. Without resetting the timer just to keep from the point it was to count up or down depending on the flag. If True it should count up, if false it should count down. ( The flag is set to false in the top of the form ).
Now the way it is it's counting only back (down).
This the timer1 tick event:
private void timer1_Tick(object sender, EventArgs e)
{
if (hours == 0 && mins == 0 && secs == 0)
{
timer1.Stop();
MessageBox.Show(new Form() { TopMost = true }, "Times up!!! :P", "Will you press OK? :P", MessageBoxButtons.OK, MessageBoxIcon.Information);
textBox1.Text = "00";
textBox2.Text = "00";
textBox3.Text = "00";
textBox3.Enabled = true;
textBox2.Enabled = true;
textBox1.Enabled = true;
button1.Enabled = true;
lblHr.Text = "00";
lblMin.Text = "00";
lblSec.Text = "00";
button2.Enabled = false;
button3.Enabled = false;
}
else
{
if (secs < 1)
{
secs = 59;
if (mins < 1)
{
mins = 59;
if (hours != 0)0
hours -= 1;
}
else mins -= 1;
}
else secs -= 1;
if (hours > 9)
lblHr.Text = hours.ToString();
else lblHr.Text = "0" + hours.ToString();
if (mins > 9)
lblMin.Text = mins.ToString();
else lblMin.Text = "0" + mins.ToString();
if (secs > 9)
lblSec.Text = secs.ToString();
else lblSec.Text = "0" + secs.ToString();
}
}
private void button4_Click(object sender, EventArgs e)
{
count_up_down = true;
}
First, change the representation of time from hours, minutes, and seconds to plain seconds. Set the text in labels by dividing seconds by 60 and 3600, like this:
int hours = totalSeconds / 3600;
int minutes = totalSeconds / 60;
int seconds = totalSeconds % 60;
Add an integer instance variable called step, and set it to negative 1:
private int step = -1;
On button click, change the sign of the step variable:
step = -step;
Now all you need to do is to change the code to use totalSeconds += step instead of xyz -= 1 - and you are done!
DateTime start = DateTime.MinValue;
TimeSpan oldTime = TimeSpan.Parse("00:00:00");
tm = new Timer();
tm.Tick += new EventHandler(tm_Tick);
void tm_Tick(object sender, EventArgs e)
{
TimeSpan runTime = DateTime.Now - start;
lblTimer.Text = string.Format("{1:D2}:{2:D2}:{3:D2}",
runTime.Days,
runTime.Hours,
runTime.Minutes,
runTime.Seconds);
}
Hope the above code will help you.