Click here to Skip to main content
15,910,603 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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

C#
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();


    }


What I have tried:

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

        }


and

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

        }
Posted
Updated 14-Feb-19 4:16am
Comments
Richard Deeming 14-Feb-19 10:22am    
REPOST
This is the same question you posted yesterday:
https://www.codeproject.com/Questions/1277212/How-do-I-refresh-form-with-timer[^]

 
Share this answer
 
v2
Comments
el_tot93 14-Feb-19 2:26am    
thx bro but i don't understand anything
RickZeeland 14-Feb-19 3:54am    
Your timer starts a separate Thread which calls Timer1_Tick(), here you need to check InvokeRequired before doing things on the main UI (User Interface) thread.
Sorry about that, but this ugly construction is something unavoidable and you will have to learn it to become a good C# programmer.
The reason it's opening another form is because you're initializing a new instance of the form by having
Graph1 Sistema = new Graphh1();


Try the following

private void RefreshMyForm()
        {
            if (this.Visible)
            {
               this.Hide();
            }
            else
            {
               this.Show();
            }

        }


By not initializing a new instance you're working with the one you already have. It doesn't actually closes the form but rather makes it invisible after a minute and after another minute makes it visible again.

I think to close and open the form with the timer you have on the form would be a bit difficult. You'll have to work through the links that Rick posted.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900