我有一个表单,单击按钮时它是打开的。现在我想在PC的时间基础上打开它。当时间在上午10:00时,该表格应在MDI父级中自动打开。当时间在晚上04:00时,它将自动关闭。...
请帮助
答案 0 :(得分:1)
要管理时间,您需要控制计时器。下面的示例代码将帮助您实现要求。
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;
}
}
}