如何使用计时器执行按钮代码
问题描述:
我想让make timer自动执行按钮写的代码,而不是单击按钮,任何人都可以帮忙吗?
i want make timer to execute code written in button automatically instead of click the button, cauld any one help me ?
答
有两种方法可以做到这一点:糟糕的想法,但很简单的方法:执行Button.PerformClick方法,这将导致按钮作出反应,就像用户点击它一样。
更好方法是将按钮事件处理程序中的代码提取到方法中,并从两个位置调用它。这样,你可以随意删除按钮,或者在以后用不同的控件替换它,而不会影响定时器代码。
There are two ways to do this: the poor-idea-but-simple way: execute the Button.PerformClick method which will cause the button to react as if the user had clicked it.
The better way is to extract the code from your button event handler into a method, and call that from the two places. That way, you are free to remove the button, or replace it with a different control at a later date without affecting the timer code.