[英]How to set time interval to button_click in C# windows form application
[英]How to Switch to another form after seconds time interval using C# Windows Form?
我尝试在线搜索此问题并获得此代码
private void Form1_Load(object sender, EventArgs e)
{
Timer MyTimer = new Timer();
MyTimer.Interval = 10000;
MyTimer.Tick += new EventHandler(MyTimer_Tick);
MyTimer.Start();
}
private void MyTimer_Tick(object sender, EventArgs e)
{
MyTimer.Stop();
this.Hide();
frm_Start frm_Start = new frm_Start();
frm_Start.ShowDialog();
this.Close();
}
这是有效的,但我的问题是当我使用其他形式时,它也会在 10 秒后关闭。
我想象的是,当您离开特定表单 10 秒钟时,我希望它切换到主表单。 仅针对该特定形式,而不是所有形式。
一种简单的方法是在初始化表单#2 时传递一个参数,以指示该表单不应运行计时器。
例如,如果这是您的 Form #2 的当前构造函数:
public frm_Start() {
Timer MyTimer = new Timer();
MyTimer.Interval = 10000;
MyTimer.Tick += new EventHandler(MyTimer_Tick);
MyTimer.Start();
}
private void MyTimer_Tick(object sender, EventArgs e)
{
MyTimer.Stop();
this.Hide();
frm_Start frm_Start = new frm_Start();
frm_Start.ShowDialog();
this.Close();
}
然后,将构造函数更改为接受一个布尔标志,当设置为 false 时,将禁用计时器的初始化,如下所示:
public frm_Start(bool startTimer) {
if (startTimer == true){
Timer MyTimer = new Timer();
MyTimer.Interval = 10000;
MyTimer.Tick += new EventHandler(MyTimer_Tick);
MyTimer.Start();
}
}
// never called, since MyTimer is never initialized
private void MyTimer_Tick(object sender, EventArgs e)
{
MyTimer.Stop();
this.Hide();
frm_Start frm_Start = new frm_Start();
frm_Start.ShowDialog();
this.Close();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.