Click here to Skip to main content
15,922,166 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
How can i run the time counter on form1 as well as the form 2. currently my code for running the counter on form 1 is


What I have tried:

 **form1 timer**

   private void UpdateTime()
    {
      
        lbltime.Text = GetTimeString(watch.Elapsed);
    }

    private string GetTimeString(TimeSpan elapsed)
    {
        string result = string.Empty;

        result = string.Format("{0:00}:{1:00}:{2:00}",
            elapsed.Hours,
            elapsed.Minutes,
            elapsed.Seconds,
            elapsed.Milliseconds);

        return result;
    }

    **form 2**
    lblcountertime=GetTimeString(watch.Elapsed);
    but it is not working.
I want that counting of lbltime counting "00:01:10" to run on form 2 as well. how can i achive that please guide me.
Posted
Updated 7-Jan-21 2:39am

You should use a Timer Class (System.Timers) | Microsoft Docs[^], which will allow you to update your counters at regular intervals.
 
Share this answer
 
Comments
Member 14852747 7-Jan-21 6:40am    
I use this class but it is not working
public class MyTime
{
public string GetTimeString(TimeSpan elapsed)
{
string result = string.Empty;

result = string.Format("{0:00}:{1:00}:{2:00}",
elapsed.Hours,
elapsed.Minutes,
elapsed.Seconds,
elapsed.Milliseconds);

return result;
}
}
Richard MacCutchan 7-Jan-21 6:57am    
Your format string contains only three fields, but you are passing four values.
You need to pass a reference to watch to Form2 when you display it from Form1 or use a static variable to hold it.

The first is difficult, because TimeSpan is a struct and thus a value type, so every time you use it you take a copy of the current value.
The second if simple, but I still wouldn't use it like that.
Instead, create a static DateTime which you preset when you start the timer - either to DateTime.Now for a "up" timer, or the finish time for a "down" timer. Then each form can use that independently to display it's information by generating a TimeSpan by subtraction using the current time.

And by the way ... you are only displaying three items via your call to string.Format, but you pass four items as parameters ...
 
Share this answer
 
Comments
Member 14852747 7-Jan-21 6:55am    
how to pass a reference?
OriginalGriff 7-Jan-21 7:13am    
As I said, passing a reference to a value type is complicated.
Have a look here:
Using struct and class - what's that all about?[^]
It explains the difference.

To pass a reference to a value type, you would need to embed it in a reference type and pass the reference to that. Which is messy at best!
First of all, i'd suggest to read this: Five Common Daylight Saving Time Antipatterns of .NET Developers[^]

If you want to measure elapsed time, i'd recommend to use Stopwatch[^] together with Timer[^].

Steps to do:


Form 1


1. Drop Timer (timer1) on the form
2. Set the following properties: Interval=100, Modifiers=Internal
3. Add Label (label1) and set property Text="00:00:00"
4. Button (button1) and set property Text="Open form 2"
5. Paste below code:
C#
public partial class Form1 : Form
{

    private readonly Stopwatch sw = new Stopwatch();

    public Form1()
    {
        InitializeComponent();
        sw.Start();
        timer1.Start();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        label1.Text = sw.Elapsed.ToString(@"hh\:mm\:ss");
    }

    public string ElapsedTime
    {
        get => label1.Text;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 frm = new Form2(this);
        frm.Show();
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        timer1.Stop();
        sw.Stop();
    }

}


Form 2


1. Add Label (label1) and set property Text="00:00:00"
2. Button (button1) and set property Text="Close"
3. Paste below code:
C#
public partial class Form2 : Form
{
    Form1 frm;
    public Form2(Form1 f)
    {
        InitializeComponent();
        frm = f;
        label1.Text = frm.ElapsedTime;
        frm.timer1.Tick += OnTimerTick;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        frm.timer1.Tick -= OnTimerTick;
        this.Close();
    }

    private void OnTimerTick(object sender, EventArgs e)
    {
        label1.Text = frm.ElapsedTime;
    }

}


Build project and have a fun!
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900