none
Window wont close RRS feed

  • Question

  • When  progressBar1 reaches 90 the TelaInicial opens but the Form1 wont close and I cant figure out the reason. Any suggestions ?

    Thanks

    namespace VASEng
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void timer1_Tick(object sender, EventArgs e)
            {
                progressBar1.Increment(1);

                progressBar1.Increment(1);
                if (progressBar1.Value == 90)
                {
                    Form1 carre = new Form1();      
                    carre.Close();

                    TelaInicial telini = new TelaInicial();
                    telini.Show();


                }
                 else return;

                                                                                     
    Sunday, March 4, 2018 3:58 AM

Answers

All replies

  • You are creating a new instance instead of closing the already opened one, just call close using this as we are inside the Form1 class so we can access it's object using this :

      TelaInicial telini = new TelaInicial();
      telini.Show();
    
      this.Close();

    Also, the following post is also helpful :

    https://stackoverflow.com/questions/5548746/c-sharp-open-a-new-form-then-close-the-current-form

    Hope it helps.


    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites



    Sunday, March 4, 2018 5:45 AM
  • When I used this both windows closed, but I found the answer in the link u posted. Thanks!
    Sunday, March 4, 2018 3:18 PM
  • If Form1 is the main form then closing it might (probably would) cause the application to also close.

    Technically speaking, if your Main method in the Program class has the following then when Form1 closes the message loop ends and the application ends.

    Application.Run(new Form1());

    There are multiple solutions and I don't know what the requirements are for your application. One of the following are possibilities:

    • In the Main method use a second message loop
    • Hide Form1 instead of closing it when it is not needed
    • Use a special class that supports multiple main windows (I forget the details but I can find it)

    Hiding Form1 would be the easiest for me to explain and for you to understand but you might want to or need to do it one of the other ways.



    Sam Hobbs
    SimpleSamples.Info

    Sunday, March 4, 2018 8:29 PM
  • Did you read my response? I said that closing Form1 might (probably would) cause the application to also close. Did you read that?


    Sam Hobbs
    SimpleSamples.Info

    Tuesday, March 6, 2018 1:05 AM
  • I guess I over-reacted. The Stackoverflow link does provide very much information and does certainly say the same thing as what I said. I over-reacted to the suggestion to close the main form.


    Sam Hobbs
    SimpleSamples.Info

    Tuesday, March 6, 2018 1:18 AM