Hey this is my first time here and I already lover these forums. I am new to C# and I have a problem with this Timer I am making. I found this somewhere else on the internet and gave it a shot but changed a few things. I got it to work with the original download after changing the things i didnt like about it, but now that I am doing it on my own script I cant get it right. I've opened the original file(that i modified) and compared andI can't find a difference between the two.
What is supposed to happen is that the user will enter an amont of time in the textboxes and it will be converted to time. When they hit start they the time will be shown on the corresponding labels. Pause button should pause it and the Stop button will clear the fields and stop the time entirely.However when I run it nothing happens. It doesn't even tell me to put numbers in all of text boxes. I really need your help here. THANKS!
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;
namespaceCounter
{
publicpartialclassCounter1 : Form
{
publicint seconds;
publicint minutes;
publicint hours;
publicbool paused;
publicCounter1()
{
InitializeComponent();
}
privatevoidCntrStart_Click(object sender, EventArgs e)
{
if (paused != true)
{
if ((MinTxtBox.Text != "") && (SecTxtBox.Text != ""))
{
Timer1.Enabled = true;
CntrPause.Enabled = true;
CntrStart.Enabled = false;
CntrStop.Enabled = true;
try
{
minutes = System.Convert.ToInt32(MinTxtBox.Text);
seconds = System.Convert.ToInt32(SecTxtBox.Text);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
else
{
MessageBox.Show("Please Enter Values in ALL Boxes!");
}
}
else
{
Timer1.Enabled = true;
paused = false;
CntrStart.Enabled = false;
CntrPause.Enabled = true;
}
}
privatevoidTimer1_Tick(object sender, EventArgs e)
{
//Verifying that the time specified has not passed yetif ((minutes == 0) && (hours == 0) && (seconds == 0))
{
/*this makes it so that if the time has passed it will clear all the fields
and also shows the message notifying that time is done*/
PrdTimer.Enabled = false;
MessageBox.Show(SecTxtBox.Text);
CntrPause.Enabled = false;
CntrStop.Enabled = false;
CntrStart.Enabled = true;
MinTxtBox.Clear();
SecTxtBox.Clear();
MinTxtBox.Enabled = true;
SecTxtBox.Enabled = true;
MinLbl.Text = "00";
SecLbl.Text = "00";
}
else
{
if (seconds < 1)
{
seconds = 59;
if (minutes == 0)
{
minutes = 59;
if (hours != 0)
hours -= 1;
}
else
{
minutes -= 1;
}
}
else
seconds -= 1;
/*Displays the current values of hours,minutes, and seconds
in the appropriate fields*/
MinLbl.Text = minutes.ToString();
SecLbl.Text = seconds.ToString();
}
}
privatevoidCntrPause_Click(object sender, EventArgs e)
{
//This section control pausing the timer
CntrTimer.Enabled = false;
paused = true;
CntrPause.Enabled = false;
CntrStart.Enabled = true;
}
privatevoidCntrStop_Click(object sender, EventArgs e)
{
//This section controls stopping the timer completley
CntrTimer.Enabled = false;
paused = false;
CntrPause.Enabled = false;
CntrStop.Enabled = false;
CntrStart.Enabled = true;
MinTxtBox.Clear();
SecTxtBox.Clear();
MinTxtBox.Enabled = true;
SecTxtBox.Enabled = true;
MinLbl.Text = "00";
SecLbl.Text = "00";
}
}
}
ohh also the timers interval is set at 1thousand
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.
You sould really use Sytem.Timers.Time is much more effeicent. In this timer it's not a Tick, it's Elapsed, put one on your form and here's some code if the user input is in second. Hope this make spark an idea.
and the stop button would just turn the timer1.Enabled = false;, to stop it. It could also clear textBox1.Text = "";. and set label1.Text = "0".
I hope that helps since the interval is 1000 (1 second) everytime it elpases, it adds one to the total count and stores that in label1 and label2 is just the time left (label1 - textBox1).
Roei BarRank #1,271 · 1.4K reputationPosted Aug 7, 2009, 1:05 PM EDT
c c17 reputationPosted Aug 8, 2009, 1:46 PM EDT
private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { int checkcount = int.Parse(label1.Text); if (checkcount == int.Parse(textBox1.Text)) { label1.Text = "Done!"; textBox1.Text = "0" timer1.Enabled = false; } else { int count = int.Parse(label1.Text) + 1; label1.Text = count.ToString(); int timeleft = int.Parse(textBox1.Text) - count; label2.Text = timeleft.ToString() + "Seconds Left"; } }And for the button1 (start):
private void button1_Click(object sender, EventArgs e) { label1.Text = "0" timer1.Enabled = true; }and the stop button would just turn the timer1.Enabled = false;, to stop it. It could also clear textBox1.Text = "";. and set label1.Text = "0".
I hope that helps since the interval is 1000 (1 second) everytime it elpases, it adds one to the total count and stores that in label1 and label2 is just the time left (label1 - textBox1).