如何使用计时器刷新表单? [英] How do I refresh form with timer?

查看:124
本文介绍了如何使用计时器刷新表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void Form_Load(object sender, EventArgs e)
{
     timer1 = new System.Windows.Forms.Timer();
     timer1.Interval = 900000;//5 minutes
     timer1.Tick += new System.EventHandler(Timer1_Tick);
}

private void Button1_Click(object sender, EventArgs e)
{         
    if (!timer1.Enabled)
        timer1.Start();
}

private void Timer1_Tick(object sender, EventArgs e)
{
    //do whatever you want 
     RefreshMyForm();
}


private void RefreshMyForm()
{
    
    this.close();

   
    Graph1 graph = new Graph1();
    graph.Show();

    

}





我的尝试:



i尝试使用此代码刷新我的关闭并打开新的回合当我使用此代码时它在后台打开多种形式





What I have tried:

i try this code to refresh my from to close it and open new one bout when i use this code it open many form in the background

private void RefreshMyForm()
        {
            this.Hide();
            var Graph1 = new Graph1();
            Graph1.Closed += (s, args) => this.Close();
            Graph1.Show();

        }











and

private void RefreshMyForm()
        {
            this.Hide();
            Graph1 sistema = new Graph1();
            sistema.ShowDialog();
            this.Close();

        }

推荐答案

问题是,如果这是你的主要表格,那么关闭它将结束你的应用。这也将关闭所有子表单。



为什么要关闭表单,哪些表单?



如果您的图表想要刷新,那么您的关闭并重新打开方法可以起作用:

The problem is that if this is your main form, then closing it will end your application. Which will close all child forms as well.

Why do you want to close forms, and which ones?

If it's your Graph to want to "refresh" then your "close and reopen" method could work:
private Graph myGraph = null;
private void RefreshMyForm()
{
    if (myGraph != null) 
    {
        myGraph.Close;
        myGraph = null;
    }
    myGraph = new Graph1();
    myGraph.FormClosing += new Graph_Closing;
    myGraph.Show();
}
private void Graph_Closing(object sender, EventArgs e)
{
    myGraph = null;
}

只需图表关闭并重新打开。



但更好的方法是在图表中添加Refresh方法形成,并调用它以便它可以刷新自己。

And just the graph will close and reopen.

But a better way is to add a Refresh method to your Graph form, and call that so that it can refresh itself.


这篇关于如何使用计时器刷新表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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