从另一个表单启动计时器

时间:2012-10-01 18:10:49

标签: c# winforms events stopwatch

在我的项目中,我有两种形式mainFormtestingForm。 在mainForm我有button1,在testingForm,我有:

Stopwatch measure = new Stopwatch();

当用户点击button1时,我希望measure秒表启动,并使用它制作其他事件。我怎样才能做到这一点?我研究了很多,但没有任何帮助...

4 个答案:

答案 0 :(得分:1)

您可以将计时器放入mainFormtestingForm都可以使用的范围内,也可以在应用程序级别使用。

答案 1 :(得分:1)

在testingForm中

Stopwatch measure = new Stopwatch();

public Stopwatch Watch { get { return measure; } }

在mainForm中

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

答案 2 :(得分:1)

使秒表成为testsform的属性。单击该按钮时,在mainform中创建新的秒表,然后将其分配给testingform属性

测试代码

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

mainform代码

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

答案 3 :(得分:0)

假设您希望主表单包含并控制程序的所有方面(秒表或任何相关内容),您可以关注this example

你唯一需要改变的是让秒表成为MainForm的一个属性,并让Form2通过引用调用该动作。

相关问题