I need to display the elapsed time dynamically. My code will pop up a message based on an interval value.
public void button1_Click(object sender, EventArgs e)
{
this.TopMost = true;
DialogResult result1 = MessageBox.Show("Add some notes to your current ticket?",
"Add Notes",
MessageBoxButtons.YesNo);
if (result1 == DialogResult.Yes)
{
Timer tm;
tm = new Timer();
int minutes = int.Parse(textBox2.Text);
tm.Interval = (int)TimeSpan.FromMinutes(minutes).TotalMilliseconds;
tm.Tick += new EventHandler(button1_Click);
tm.Enabled = true;
string pastebuffer = DateTime.Now.ToString();
pastebuffer = "### Edited on " + pastebuffer + " by " + txtUsername.Text + " ###";
Clipboard.SetText(pastebuffer);
tm.Start();
}
else if (result1 == DialogResult.No)
{
}
this.TopMost = false;
}
If I have defined 15 mins in my interval how do i get the countdown to show in a label?
I wouldn't muck about with timers. I'd use Microsoft's Reactive Framework for this. Just NuGet "Rx-Winforms" to get the bits. Then you can do this:
This will automatically create a countdown timer that will update the text in
label1
with the following values:If you want to stop this before the timer runs out the
Subscribe
method returns anIDisposable
that you can just call.Dispose()
.You should try to count the 15 minutes. For example, if your using a Label (Label1) you should count it with a timer. Just use a timer and count every tick (1000 milliseconds) +1
You should store end-time in a filed at form level and then in
Tick
event handler of the timer check the difference between the end-time and now and update a label which you want to show count-down timer: