Reputation: 13
I have a simple C# program (I am using Visual studio 2010) -simply a button on a form-as shown in the link at the very bottom below-that when this button is pressed it shows the time passing every second in a message box.
This is no mistake in this program its given below and fully working. It works by setting the variable starttodisplaytimeronscreen to true once the button is pressed-this is a flag-and once its set to true it makes it possible to show the message box to be shown every second. Here is the working code (that the complete program I am using)
using System;
using System.Timers;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace finitetimerprogram
{
public partial class Form1 : Form
{
int thetimeinsecs = 0;// is the number of seconds as an integer that have elapsed since button pressed
bool starttodisplaytimeronscreen = false;// this is a flag that set to true if button is pressed
System.Timers.Timer mytimer = new System.Timers.Timer();
private void customfn(object source, ElapsedEventArgs e)
{
if (starttodisplaytimeronscreen == true)
{ // starttodisplaytimeronscreen becomes true if the button is clicked
thetimeinsecs = thetimeinsecs + 1;
MessageBox.Show("seconds since pressed= " + thetimeinsecs.ToString(), "seconds since pressed= " + thetimeinsecs.ToString());
}
}
public Form1()
{ // this function is called continually from since when the form is shown onscreen
InitializeComponent();
mytimer.Elapsed += new ElapsedEventHandler(customfn);
mytimer.Interval = 1000;
}
private void button1_Click(object sender, EventArgs e)
{
starttodisplaytimeronscreen = true;
mytimer.Start();
}
}
}
Now I want to modify this program so it stops showing the timer after 5 seconds- in other words the button click activates a 5 second timer which is shown to on screen as five message boxes that show the seconds 1,2,3,4 and 5. Then once the timer stops if the user presses the button again (and again) it starts the 5 second timer again (it is assumed in this situation the button is clicked only once for every time the user want to create a 5 second timer).
Ive done this by adding the simple code
if (thetimeinsecs > 5)
{// this if statement stop the timer after 5 seconds but doesnt work
starttodisplaytimeronscreen = false;// supposed to stop displaying the message box after 5 secods
thetimeinsecs = 0;
mytimer.Stop();
}
which resets the timer after 5 seconds, resets the flag starttodisplaytimeronscreen to false. Resets thetimeinsecs back to zero, and stops the timer with mytimer.Stop(). The problem I am having is such a simple code to modify the working code (initially given above) to make a 5 second timer doesnt work. So my question is why doesnt it work? -and whats an easy way to fix it?
Here below is the complete non working code - just to be clear.
using System;
using System.Timers;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace finitetimerprogram
{
public partial class Form1 : Form
{
int thetimeinsecs = 0;// is the number of seconds as an integer that have elapsed since button pressed
bool starttodisplaytimeronscreen = false;// this is a flag that set to true if button is pressed
System.Timers.Timer mytimer = new System.Timers.Timer();
private void customfn(object source, ElapsedEventArgs e)
{
if (starttodisplaytimeronscreen == true)
{ // starttodisplaytimeronscreen becomes true if the button is clicked
thetimeinsecs = thetimeinsecs + 1;
MessageBox.Show("seconds since pressed= " + thetimeinsecs.ToString(), "seconds since pressed= " + thetimeinsecs.ToString());
}
if (thetimeinsecs > 5)
{// this if statment stop the timer after 5 seconds but doesnt work
starttodisplaytimeronscreen = false;// supposed to stop displaying the message box after 5 secods
thetimeinsecs = 0;
mytimer.Stop();
}
}
public Form1()
{ // this function is called continually from since when the form is shown onscreen
InitializeComponent();
mytimer.Elapsed += new ElapsedEventHandler(customfn);
mytimer.Interval = 1000;
}
private void button1_Click(object sender, EventArgs e)
{
starttodisplaytimeronscreen = true;
mytimer.Start();
}
shows what happens after 6 seconds since the button was pressed
Upvotes: 0
Views: 82
Reputation: 20095
The simple fix could be by swapping lines your function as shown below:
private void customfn(object source, ElapsedEventArgs e)
{
if (thetimeinsecs >= 5)
{// this if statment stop the timer after 5 seconds but doesnt work
starttodisplaytimeronscreen = false;// supposed to stop displaying the message box after 5 secods
thetimeinsecs = 0;
mytimer.Stop();
}
if (starttodisplaytimeronscreen == true)
{ // starttodisplaytimeronscreen becomes true if the button is clicked
thetimeinsecs = thetimeinsecs + 1;
MessageBox.Show("seconds since pressed= " + thetimeinsecs.ToString(), "seconds since pressed= " + thetimeinsecs.ToString());
}
}
If you want to improve it further than you can get rid of starttodisplaytimeronscreen
with slight change in logic. I'll leave that for you.
Upvotes: 1