从表格 2 中控制表格 1 中的计时器,C# [英] Control timer in form 1 from form 2, C#

查看:25
本文介绍了从表格 2 中控制表格 1 中的计时器,C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

表单中的标签显示计时器的计数.现在我想使用表格 2 停止、启动和重置它.我该怎么做.请帮助

A label in form is displaying the count of timer. Now i want to stop,start and reset it using form 2. How can i do this.plz help

推荐答案

表单只是类,表单 2 上的计时器是该类中的一个对象.

Forms are just classes, and the timer on Form 2 is an object inside that class.

你可以把你的timer的Modifiers属性改成public,然后在Form 1里面实例化Form 2,调用Form 2的Show()方法,然后访问现在公开的计时器对象.

You can change the Modifiers property of your timer to public, and then instantiate Form 2 inside Form 1, call the Show() method of Form 2, and then access your timer object which is now public.

所以你有一个包含 2 个表单的项目,如下所示:

So you have a project with 2 forms like so:

像这样在 Form 1 中创建一个按钮:

Create a button in Form 1 like so:

在 Form 2 上放置一个计时器对象并像这样更改访问修饰符:

Place a timer object on Form 2 and change the access modifier like so:

然后将以下代码放在表单一的按钮下:

Then put the following code under your button in form one:

private void button1_Click(object sender, EventArgs e)
{
    Form2 f2 = new Form2();
    f2.Show();
    f2.timer1.Enabled = true;
}

现在您可以启动表单 2 并从表单 1 访问表单 2 上计时器的所有属性.

Now you can launch form 2 and access all of the properties on the timer on form 2 from form 1.

这有帮助吗?

这篇关于从表格 2 中控制表格 1 中的计时器,C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆