0

Here is what I have so far. It is working, however now they have requested to have separate buttons for start, stop and reset. I am totally new to C# and was hoping someone could help me separate the one button I have into 3 buttons instead:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Text;

namespace stopwatch
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class NextGenTimer : System.Windows.Forms.Form
{
DateTime da ;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Timer timer1;
private Label Ver;
private PictureBox pictureBox1;
private System.ComponentModel.IContainer components;

public NextGenTimer()
{
//
// 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()
{
this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(NextGenTimer));
this.label1 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.Ver = new System.Windows.Forms.Label();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
// 
// label1
// 
this.label1.BackColor = System.Drawing.SystemColors.ControlLightLight;
this.label1.Font = new System.Drawing.Font("Harrington", 18F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label1.ForeColor = System.Drawing.Color.DarkBlue;
this.label1.Location = new System.Drawing.Point(16, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(224, 56);
this.label1.TabIndex = 0;
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.label1.Click += new System.EventHandler(this.label1_Click);
// 
// button1
// 
this.button1.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, ((byte)(178)));
this.button1.Location = new System.Drawing.Point(12, 78);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(223, 40);
this.button1.TabIndex = 1;
this.button1.Text = "Start";
this.button1.Click += new System.EventHandler(this.button1_Click);
// 
// timer1
// 
this.timer1.Interval = 1;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
// 
// Ver
// 
this.Ver.AutoSize = true;
this.Ver.Location = new System.Drawing.Point(196, 153);
this.Ver.Name = "Ver";
this.Ver.Size = new System.Drawing.Size(44, 13);
this.Ver.TabIndex = 2;
this.Ver.Text = "Ver. 1.0";
// 
// pictureBox1
// 
this.pictureBox1.Image = global::stopwatch.Properties.Resources.next;
this.pictureBox1.Location = new System.Drawing.Point(1, 124);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(153, 41);
this.pictureBox1.TabIndex = 3;
this.pictureBox1.TabStop = false;
this.pictureBox1.Click += new System.EventHandler(this.pictureBox1_Click);
// 
// NextGenTimer
// 
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.BackColor = System.Drawing.SystemColors.ScrollBar;
this.ClientSize = new System.Drawing.Size(254, 167);
this.Controls.Add(this.pictureBox1);
this.Controls.Add(this.Ver);
this.Controls.Add(this.button1);
this.Controls.Add(this.label1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.Name = "NextGenTimer";
this.Text = "StopWatch";
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main() 
{
Application.Run(new NextGenTimer());
}

private void timer1_Tick(object sender, System.EventArgs e)
{
TimeSpan span = DateTime.Now.Subtract(da);
this.label1.Text = span.Hours.ToString() + " : " + span.Minutes.ToString() + " : " + span.Seconds.ToString() + " : " 
+ span.Milliseconds.ToString();
}

private void button1_Click(object sender, System.EventArgs e)
{
if(this.timer1.Enabled)
{
timer1.Stop();
button1.Text = "Start";
}
else
{
da = DateTime.Now;
timer1.Start();
button1.Text = "Stop";
}
}



private void button2_Click(object sender, EventArgs e)
{
}
private void lnkReset_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{


}

private void label1_Click(object sender, EventArgs e)
{

}

private void pictureBox1_Click(object sender, EventArgs e)
{

}
}
}
2
Contributors
1
Reply
2
Views
4 Years
Discussion Span
Last Post by lolafuertes
0

I hope you don't expect that some one will write the code for you, but i'll try to enlight on how to proceed.

Basically you'll need to add 2 other buttons in the design of your form:

Button2 -> Stop
Button3 -> Reset

If you double click on the buttons, this will generate the click event for you.

On button1_click , replace the current code by: let the da assignement and do the timer start.

On button2_Click do only the timer stop.

On button3_click if the timer is enabled let the da assignement.

Hope this helps.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.