 |
|
 |
Micheal Level: Whizz Kid

 Registered: 17-11-2004 Posts: 16
|
C# timer
Hello All,
I am new to programming and C# is my first language to learn. I am trying to find a way of stopping my stop watch application when it reaches the number of seconds; I enter in the text box. It works when the value is less the 59 sec. I want to be able to go as high as 120 sec which is equals to 2 min.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace WindowsApplication3
{
public class inter
{
int num;
public int Number
{
get
{
return num;
}
set
{
num = value;
}
}
}
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
DateTime da;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label1;
private System.Timers.Timer tick;
private System.Windows.Forms.TextBox textBox;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.button1 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.tick = new System.Timers.Timer();
this.textBox = new System.Windows.Forms.TextBox();
((System.ComponentModel.ISupportInitialize)(this.tick)).BeginInit();
this.SuspendLayout();
//
// button1
//
this.button1.Dock = System.Windows.Forms.DockStyle.Bottom;
this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button1.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.button1.Location = new System.Drawing.Point(0, 80);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(292, 48);
this.button1.TabIndex = 0;
this.button1.Text = "START";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label1
//
this.label1.Dock = System.Windows.Forms.DockStyle.Top;
this.label1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.label1.Font = new System.Drawing.Font("Forte", 30F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label1.Location = new System.Drawing.Point(0, 0);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(292, 56);
this.label1.TabIndex = 1;
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// tick
//
this.tick.Interval = 1;
this.tick.SynchronizingObject = this;
this.tick.Elapsed += new System.Timers.ElapsedEventHandler(this.tick_Elapsed);
//
// textBox
//
this.textBox.Dock = System.Windows.Forms.DockStyle.Top;
this.textBox.Location = new System.Drawing.Point(0, 56);
this.textBox.Name = "textBox";
this.textBox.Size = new System.Drawing.Size(292, 20);
this.textBox.TabIndex = 2;
this.textBox.Text = "";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.BackColor = System.Drawing.Color.LightSteelBlue;
this.ClientSize = new System.Drawing.Size(292, 128);
this.Controls.Add(this.textBox);
this.Controls.Add(this.label1);
this.Controls.Add(this.button1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
//this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "Form1";
this.Opacity = 0.9;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
((System.ComponentModel.ISupportInitialize)(this.tick)).EndInit();
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
if(this.tick.Enabled)
{
tick.Stop();
button1.Text = "Start";
}
else
{
da = DateTime.Now;
tick.Start();
button1.Text = "Stop";
}
}
private void tick_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
inter go = new inter();
go.Number = System.Int32.Parse(this.textBox.Text);
TimeSpan sp = DateTime.Now.Subtract(da);
if (sp.Seconds == go.Number)
{
tick.Stop();
MessageBox.Show(sp.TotalSeconds.ToString());
}
this.label1.Text = sp.Hours.ToString() + ":" + sp.Minutes.ToString() + ":" + sp.Seconds.ToString();
}
}
}
|
|
|
17-11-2004 at 09:01 AM |
|
|
Goran Level: Moderator
 Registered: 16-05-2002 Posts: 1681
|
Re: C# timer
I looked only at tick_elapsed subroutine and found a mistake there. You used Seconds property which maximum value is 59. THe correct property to use is TotalSeconds
| if (sp.TotalSeconds == go.Number) |
____________________________
If you find the answer helpful, please mark this topic as solved.
|
|
18-11-2004 at 08:54 PM |
|
|
Goran Level: Moderator
 Registered: 16-05-2002 Posts: 1681
|
Re: C# timer
First, two things. You shouldnt create inter object everytime tick_elapsed event is raised. So, declaring your go object at the form level (like da object) would be better. Also,
| go.Number = System.Int32.Parse(this.textBox.Text); |
should be in the button_click event, no need to set this property everytime Elpased event is raised.
And finally, in order your code to work, oyu should use >= instead of == when comparing TotalSeconds and Number values. This is because TotalSeconds property returns a double type, which can, for instance, be 61.012, which is not equal to 61. So, just change your code to
| if (sp.TotalSeconds >= go.Number) |
and it will work.
____________________________
If you find the answer helpful, please mark this topic as solved.
|
|
20-11-2004 at 02:33 AM |
|
|
|
|
 |
 |