To get this to work, I added my calling definition of the timer object to the main class of the code (which is how the MSDN site says to assign it). Now I can call that object from any portion of the code, which is how I want it.
Now my problem is when I hit the stop button, it stops, but when I hit the start button again, it will decrease the time by the amount of times I have hit the start button, ie: I hit it once, it will decrease by one, when I stop it and start it again (twice now) it will decrease by two, if I do it again (three times now) it will decrease by three.
I don't understand why this is happening because I'm not using a variable to take away the time, I'm using 1.
Edit: When reading my post (code) I think I'm setting a new event per start button press, so that is why it is changing like it is. So now I'm looking to see how I can test to see if an event has been associate with the timer object. Be back soon I hope.
Here is the code if it will help. Again, thanks for any help/ideas/pointers/comments.
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Diagnostics;
using System.Timers;
using System.Collections;
namespace WindosApps
{
/// <summary>
/// Description of CountDown.
/// </summary>
public class CountDown : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.ListBox listBox1;
System.Timers.Timer CountDownTimer = new System.Timers.Timer();
public CountDown()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
//
// TODO: Add constructor code after the InitializeComponent() call.
//
}
[STAThread]
public static void Main(string[] args)
{
Application.Run(new CountDown());
}
#region Windows Forms Designer generated code
/// <summary>
/// This method is required for Windows Forms designer support.
/// Do not change the method contents inside the source code editor. The Forms designer might
/// not be able to load this method if it was changed manually.
/// </summary>
private void InitializeComponent() {
this.listBox1 = new System.Windows.Forms.ListBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.button3 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// listBox1
//
this.listBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)));
this.listBox1.BackColor = System.Drawing.SystemColors.Desktop;
this.listBox1.Font = new System.Drawing.Font("Baskerville Old Face", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.listBox1.ForeColor = System.Drawing.SystemColors.Window;
this.listBox1.ItemHeight = 14;
this.listBox1.Location = new System.Drawing.Point(16, 32);
this.listBox1.Name = "listBox1";
this.listBox1.SelectionMode = System.Windows.Forms.SelectionMode.MultiExtended;
this.listBox1.Size = new System.Drawing.Size(216, 186);
this.listBox1.TabIndex = 0;
//
// button1
//
this.button1.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
this.button1.Location = new System.Drawing.Point(16, 328);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(56, 24);
this.button1.TabIndex = 1;
this.button1.Text = "Start";
this.button1.Click += new System.EventHandler(this.Button1Click);
//
// button2
//
this.button2.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
this.button2.Location = new System.Drawing.Point(176, 328);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(56, 24);
this.button2.TabIndex = 1;
this.button2.Text = "Exit";
this.button2.Click += new System.EventHandler(this.Button2Click);
//
// textBox1
//
this.textBox1.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
this.textBox1.BackColor = System.Drawing.SystemColors.Info;
this.textBox1.Font = new System.Drawing.Font("CityBlueprint", 26.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(2)));
this.textBox1.ForeColor = System.Drawing.SystemColors.ActiveCaption;
this.textBox1.Location = new System.Drawing.Point(16, 256);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(216, 52);
this.textBox1.TabIndex = 2;
this.textBox1.Text = "00:00:00";
this.textBox1.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
//
// label1
//
this.label1.Anchor = System.Windows.Forms.AnchorStyles.Top;
this.label1.Location = new System.Drawing.Point(8, 8);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(240, 17);
this.label1.TabIndex = 3;
this.label1.Text = "Highlight processes to end when timer expires.";
//
// label2
//
this.label2.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
this.label2.Location = new System.Drawing.Point(8, 232);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(224, 16);
this.label2.TabIndex = 4;
this.label2.Text = "Time remaining.";
//
// button3
//
this.button3.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
this.button3.Location = new System.Drawing.Point(96, 328);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(56, 24);
this.button3.TabIndex = 1;
this.button3.Text = "Stop";
this.button3.Click += new System.EventHandler(this.Button3Click);
//
// CountDown
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.BackColor = System.Drawing.SystemColors.ActiveBorder;
this.ClientSize = new System.Drawing.Size(248, 365);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button1);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.button2);
this.Controls.Add(this.button3);
this.MaximumSize = new System.Drawing.Size(256, 700);
this.MinimumSize = new System.Drawing.Size(256, 392);
this.Name = "CountDown";
this.Text = "CountDown";
this.Load += new System.EventHandler(this.CountDownLoad);
this.ResumeLayout(false);
}
#endregion
void Button2Click(object sender, System.EventArgs e)
{
CountDownTimer.Dispose();
this.Close();
}
void CountDownLoad(object sender, System.EventArgs e)
{
Process[] ProAr = System.Diagnostics.Process.GetProcesses();
string[] StrAr = new string[ProAr.Length];
int cnt = 0;
foreach (Process Pro in ProAr) {
if (Pro.Responding) {
StrAr[cnt] = Pro.ProcessName;
++cnt;
}
}
Array.Sort (StrAr);
foreach (string Str in StrAr) {
listBox1.Items.Add (Str);
}
}
void Button1Click(object sender, System.EventArgs e)
{
if (this.textBox1.Text.Length != 8) {
MessageBox.Show ("Need to enter time in hours minutes seconds. (00:00:00)");
}
else {
//System.Timers.Timer CountDownTimer = new System.Timers.Timer();
CountDownTimer.Elapsed += new ElapsedEventHandler (SecondCount);
CountDownTimer.Interval = 1000;
CountDownTimer.Enabled = true;
}
}
private void SecondCount (object sender, ElapsedEventArgs e) {
this.textBox1.Text = SubtractSecond (this.textBox1.Text);
if (this.textBox1.Text == "00:00:00") {
foreach (string Str in this.listBox1.SelectedItems) {
foreach (Process Pro in System.Diagnostics.Process.GetProcessesByName (Str)) {
if (!Pro.CloseMainWindow()) {
Pro.Kill();
}
}
}
this.Close();
}
}
private string SubtractSecond (string CurTime) {
int Hour = Convert.ToInt32 (CurTime.Substring (0,2));
int Minute = Convert.ToInt16 (CurTime.Substring (3,2));
int Second = Convert.ToInt16 (CurTime.Substring (6,2));
string strHour = "00";
string strMinute = "00";
string strSecond = "00";
if ((Second == 1 && Minute == 0 && Hour == 0) || (Second == 0 && Minute == 0 && Hour == 0)) {
return "00:00:00";
}
if (Second == 0) {
Minute = Minute - 1;
Second = 59;
if (Minute <= 0 && Hour > 0) {
Hour = Hour - 1;
Minute = 59;
}
}
else {
Second = Second - 1;
}
if (Minute < 0) {
Minute = 0;
}
if (Hour < 0) {
Hour = 0;
}
if (Second < 10) {
strSecond = "0" + Convert.ToString (Second);
}
else {
strSecond = Convert.ToString (Second);
}
if (Minute < 10) {
strMinute = "0" + Convert.ToString (Minute);
}
else {
strMinute = Convert.ToString (Minute);
}
if (Hour < 10) {
strHour = "0" + Convert.ToString (Hour);
}
else {
strHour = Convert.ToString (Hour);
}
return strHour + ":" + strMinute + ":" + strSecond;
}
void Button3Click(object sender, System.EventArgs e)
{
CountDownTimer.Stop();
}
}
}