borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (WebService and .net)Next Topic (print queue) New Topic New Poll Post Reply
AndreaVB Forum : C# : C# timer
Poster Message
Micheal
Level: Whizz Kid


Registered: 17-11-2004
Posts: 16

icon 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
View Profile Send Email to User Show All Posts | Quote Reply
GeoffS
Level: VB Lord


Registered: 29-09-2004
Posts: 536
icon Re: C# timer

Hi,
Don't know much about C# but in view of your (unfair) criticism of this site I thought I would do my best to help.
As far as I am aware the Timer intervals are in milliseconds, so you need to add 1000 to your input of secopnds to get the timer to work at the required interval.
Also, if you are entering an interval, then changing that interval after the timer has fired the Event, then you must call the Start Method again if the AutoReset Property is not set to True.
Hope that helps you.



____________________________
multi-tasking - the ability to hang more than one app. at the same time.

18-11-2004 at 04:29 PM
View Profile Send Email to User Show All Posts | Quote Reply
Goran
Level: Moderator

Registered: 16-05-2002
Posts: 1681
icon 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
View Profile Send Email to User Show All Posts | Quote Reply
GeoffS
Level: VB Lord


Registered: 29-09-2004
Posts: 536
icon Re: C# timer

Well there you go then Micheal - If you had just been a little more patient you would have got the right answer from someone who KNOWS C#


____________________________
multi-tasking - the ability to hang more than one app. at the same time.

19-11-2004 at 08:25 AM
View Profile Send Email to User Show All Posts | Quote Reply
Micheal
Level: Whizz Kid


Registered: 17-11-2004
Posts: 16
icon Re: C# timer


Thank you, all.

I guess I was wrong about this forum. I would like to withdraw my earlier statement.

Geoffs and Goran I have tried what you asked me to change and still the timer wont stop.

My this.tick.Interval = 1000;
if (sp.TotalSeconds == go.Number);

Do you have any other way to achive the same thing.

19-11-2004 at 09:11 AM
View Profile Send Email to User Show All Posts | Quote Reply
GeoffS
Level: VB Lord


Registered: 29-09-2004
Posts: 536
icon Re: C# timer

Surely, if you set the Timer Interval to the length of time that you want it to run, and set the AutoReset Property to False, then when the Elapsed Event fires then its time to stop - no need to test any conditions and the Timer will not continue. That's what I'd do in VB anyway

____________________________
multi-tasking - the ability to hang more than one app. at the same time.

19-11-2004 at 10:29 AM
View Profile Send Email to User Show All Posts | Quote Reply
Goran
Level: Moderator

Registered: 16-05-2002
Posts: 1681
icon 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
View Profile Send Email to User Show All Posts | Quote Reply
Micheal
Level: Whizz Kid


Registered: 17-11-2004
Posts: 16
icon Re: C# timer



Thanks a lot guys, it worked........

21-11-2004 at 03:58 PM
View Profile Send Email to User Show All Posts | Quote Reply
AndreaVB Forum : C# : C# timer
Previous Topic (WebService and .net)Next Topic (print queue) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder