0

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...

LW001
  • 2,452
  • 6
  • 27
  • 36
Victor
  • 13,914
  • 19
  • 78
  • 147
  • is there any specific reason for not having the stopwatch on main form – Prabhu Murthy Oct 01 '12 at 18:12
  • yes, it is. but if this doesn't help you, I don't want to write more details, because it is just a little application that helps me to measure some data. – Victor Oct 01 '12 at 18:13

4 Answers4

1

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

John
  • 15,990
  • 10
  • 70
  • 110
  • this would be helpful I think, but could you give me more indications? – Victor Oct 01 '12 at 18:16
  • 1
    Instead of making your timer a member of one of the forms, make it a member of the application class, or some class in which both forms can access and use the timer. – John Oct 01 '12 at 18:20
1

In testingForm

Stopwatch measure = new Stopwatch();

public Stopwatch Watch { get { return measure; } }

In mainForm

testingForm frm = new testingForm();
frm.Watch.Start();
//...
frm.Watch.Stop();
Furqan Safdar
  • 16,260
  • 13
  • 59
  • 93
1

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;
        }
Spevy
  • 1,325
  • 9
  • 22
0

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.

Community
  • 1
  • 1
Christopher Bales
  • 1,069
  • 1
  • 13
  • 23