Victor
Victor

Reputation: 14622

Start timer from another form

In my project I have two forms mainForm and testingForm. In mainForm i have button1, and in testingForm, I have:

Stopwatch measure = new Stopwatch();

When the user clicks button1, i want the measure stopwatch to start, and to make other events with it. How can I do that? I researched a lot, but nothing helped...

Upvotes: 0

Views: 1955

Answers (4)

Spevy
Spevy

Reputation: 1325

Make the Stopwatch a property of testingform. When the button is clicked you create the new Stopwatch in mainform and then assign it to the testingform property

Code for testingform

 private Stopwatch _Measure;
        public Stopwatch Measure
        {
            get
            {
                return _Measure;
            } 
            set 
            { _Measure = value;
                // Do some stuff
            }
        }

Code for mainform

 private void button1_Click(object sender, EventArgs e)
        {
            Stopwatch measure = new Stopwatch();
            testingform.Measure = measure;
        }

Upvotes: 1

Christopher Bales
Christopher Bales

Reputation: 1071

Assuming your're wanting the Main Form to contain and control all aspects of the program (the stopwatch, or anything for that matter), you can follow this example.

The only thing you'll need to change is making the stopwatch a property of the MainForm and having Form2 call the action by reference.

Upvotes: 0

Furqan Safdar
Furqan Safdar

Reputation: 16708

In testingForm

Stopwatch measure = new Stopwatch();

public Stopwatch Watch { get { return measure; } }

In mainForm

testingForm frm = new testingForm();
frm.Watch.Start();
//...
frm.Watch.Stop();

Upvotes: 1

John
John

Reputation: 16007

You can bring the timer into a scope where both mainForm and testingForm can use it, maybe at the application level.

Upvotes: 1

Related Questions