Search code examples
c#timerrefresh

what is the way to close the form and open it again


i try to close the from and open it again with this code bout it didn't close the form i found it in the background and open another one for it

private void Graph_Load(object sender, EventArgs e)
    {
       System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();

        timer1.Interval = 60000;//1 minutes
        timer1.Tick += new System.EventHandler(Timer1_Tick);
        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();


    }

start refresh is what i looking for


Solution

  • All you have to do is to change RefreshMyForm() to Refresh(); and clear function RefreshMyForm().

        private void Graph_Load(object sender, EventArgs e)
        {
            {
                System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();
    
                label1.Text = DateTime.Now.ToString("HH:mm:ss");
    
                timer1.Interval = 60000;//1 minutes
                timer1.Tick += new System.EventHandler(Timer1_Tick);
                timer1.Start();
            }
        }
    
        private void Timer1_Tick(object sender, EventArgs e)
        {
            label1.Text = DateTime.Now.ToString("HH:mm:ss");
            Refresh(); // OR Invalidate(); OR Update();
        }     
    

    Here label1 is simple watch to see how form refreshed every minute