如何使用C# winform在特定时间打开新窗体
How to open new form in specific time using C# winform
我有一个表单,在单击按钮事件时它是打开的。现在我想在 PC 的时间基础上打开它。当时间在早上 10.00 时,该表单应在 MDI 父级中自动打开。当时间到了晚上 04:00 时,它应该会自动关闭....
请帮忙
要管理时间,您需要进行计时器控制。下面的示例代码将帮助您实现您的要求。
public partial class frmStackAnswers : Form
{
Timer tmr = new Timer(); //Timer to manage time
Form childForm; //Child form to display
public frmStackAnswers()
{
InitializeComponent();
Load += frmStackAnswers_Load;
}
void frmStackAnswers_Load(object sender, EventArgs e)
{
tmr.Interval = 60000;
tmr.Tick += tmr_Tick;
tmr.Start();
}
void tmr_Tick(object sender, EventArgs e)
{
//Start child form between 10 AM to 4 PM if closed
if (DateTime.Now.Hour > 10 && DateTime.Now.Hour < 16 && childForm == null)
{
childForm = new Form();
childForm.Show();
}
//Close child form after 4 PM if it is opened
else if (DateTime.Now.Hour > 16 && childForm != null)
{
childForm.Close();
childForm = null;
}
}
}
我有一个表单,在单击按钮事件时它是打开的。现在我想在 PC 的时间基础上打开它。当时间在早上 10.00 时,该表单应在 MDI 父级中自动打开。当时间到了晚上 04:00 时,它应该会自动关闭....
请帮忙
要管理时间,您需要进行计时器控制。下面的示例代码将帮助您实现您的要求。
public partial class frmStackAnswers : Form
{
Timer tmr = new Timer(); //Timer to manage time
Form childForm; //Child form to display
public frmStackAnswers()
{
InitializeComponent();
Load += frmStackAnswers_Load;
}
void frmStackAnswers_Load(object sender, EventArgs e)
{
tmr.Interval = 60000;
tmr.Tick += tmr_Tick;
tmr.Start();
}
void tmr_Tick(object sender, EventArgs e)
{
//Start child form between 10 AM to 4 PM if closed
if (DateTime.Now.Hour > 10 && DateTime.Now.Hour < 16 && childForm == null)
{
childForm = new Form();
childForm.Show();
}
//Close child form after 4 PM if it is opened
else if (DateTime.Now.Hour > 16 && childForm != null)
{
childForm.Close();
childForm = null;
}
}
}