C# Open a form from another form (too much processing memory)











up vote
-2
down vote

favorite
1












I have been looking for some time on ways to open a second form from another already shown form.



This is some piece of code that works:



frmSecond second = new frmSecond();
this.Hide();
second.ShowDialog();
this.Close();


What it does basically is to Hide() the currently opened form, then it opens another form (the ShowDialog() method). It will only Close() the currently hidden form when the form you have just created is closed.



The problem here is: this way of doing it creates an immense thread of forms. If I need to go from frmSecond to frmThird, it will maintain the first form and the frmSecond being executed in the background, while only showing the frmThird.



Then, as the frmThird is open, if I need to get back to the first form, I would use some code like:



frmFirst first = new frmFirst();
this.Hide();
first.ShowDialog();
this.Close();


And it would create another frmFirst! Then we would have three forms being executed in the background (the first frmFirst, frmSecond, and frmThird).



This method works, but uses an increasingly amount of processing memory, which may be prejudicial for any kind of project.



Is there any alternative or add up to correct this problem?



If anything is unclear, please don't bother in letting me know.
Thank you.










share|improve this question






















  • you could use using(Form f = new Form()) { f.ShowDialog(); } it will automatically dispose of form when closed
    – Aleksa Ristic
    Nov 19 at 0:02






  • 1




    Usually a winforms application will have a main form and open subsequent forms, Generally normal applications don't try to minimise the previous form, as the next child form is related to the previous one
    – TheGeneral
    Nov 19 at 0:03










  • Secondly, when you are finished with a child form you close it, if its modal, you usually have it in a using statement, control goes back to your previous (non hidden form) and you dont have to create a new form
    – TheGeneral
    Nov 19 at 0:07






  • 1




    Hide() does not hide anything from the machine, only from the user's eyes. Look here for a possible way to get ahead.
    – Hans Passant
    Nov 19 at 0:50










  • The thing is: the form is already disposed (and closed) when the user come back from it, but I have a kind of complex menu structure, so it's not only one parent form and a bunch of children. What I'm trying to do is creating a menu, that redirects to a Sign in form that will, if the user and password are correct, show the user a dashboard, that will have a lot of other options. So there are dozens of parent/children relations, that create and need this enormous amount of processing memory.
    – Estêvão Rolim
    Nov 19 at 3:00















up vote
-2
down vote

favorite
1












I have been looking for some time on ways to open a second form from another already shown form.



This is some piece of code that works:



frmSecond second = new frmSecond();
this.Hide();
second.ShowDialog();
this.Close();


What it does basically is to Hide() the currently opened form, then it opens another form (the ShowDialog() method). It will only Close() the currently hidden form when the form you have just created is closed.



The problem here is: this way of doing it creates an immense thread of forms. If I need to go from frmSecond to frmThird, it will maintain the first form and the frmSecond being executed in the background, while only showing the frmThird.



Then, as the frmThird is open, if I need to get back to the first form, I would use some code like:



frmFirst first = new frmFirst();
this.Hide();
first.ShowDialog();
this.Close();


And it would create another frmFirst! Then we would have three forms being executed in the background (the first frmFirst, frmSecond, and frmThird).



This method works, but uses an increasingly amount of processing memory, which may be prejudicial for any kind of project.



Is there any alternative or add up to correct this problem?



If anything is unclear, please don't bother in letting me know.
Thank you.










share|improve this question






















  • you could use using(Form f = new Form()) { f.ShowDialog(); } it will automatically dispose of form when closed
    – Aleksa Ristic
    Nov 19 at 0:02






  • 1




    Usually a winforms application will have a main form and open subsequent forms, Generally normal applications don't try to minimise the previous form, as the next child form is related to the previous one
    – TheGeneral
    Nov 19 at 0:03










  • Secondly, when you are finished with a child form you close it, if its modal, you usually have it in a using statement, control goes back to your previous (non hidden form) and you dont have to create a new form
    – TheGeneral
    Nov 19 at 0:07






  • 1




    Hide() does not hide anything from the machine, only from the user's eyes. Look here for a possible way to get ahead.
    – Hans Passant
    Nov 19 at 0:50










  • The thing is: the form is already disposed (and closed) when the user come back from it, but I have a kind of complex menu structure, so it's not only one parent form and a bunch of children. What I'm trying to do is creating a menu, that redirects to a Sign in form that will, if the user and password are correct, show the user a dashboard, that will have a lot of other options. So there are dozens of parent/children relations, that create and need this enormous amount of processing memory.
    – Estêvão Rolim
    Nov 19 at 3:00













up vote
-2
down vote

favorite
1









up vote
-2
down vote

favorite
1






1





I have been looking for some time on ways to open a second form from another already shown form.



This is some piece of code that works:



frmSecond second = new frmSecond();
this.Hide();
second.ShowDialog();
this.Close();


What it does basically is to Hide() the currently opened form, then it opens another form (the ShowDialog() method). It will only Close() the currently hidden form when the form you have just created is closed.



The problem here is: this way of doing it creates an immense thread of forms. If I need to go from frmSecond to frmThird, it will maintain the first form and the frmSecond being executed in the background, while only showing the frmThird.



Then, as the frmThird is open, if I need to get back to the first form, I would use some code like:



frmFirst first = new frmFirst();
this.Hide();
first.ShowDialog();
this.Close();


And it would create another frmFirst! Then we would have three forms being executed in the background (the first frmFirst, frmSecond, and frmThird).



This method works, but uses an increasingly amount of processing memory, which may be prejudicial for any kind of project.



Is there any alternative or add up to correct this problem?



If anything is unclear, please don't bother in letting me know.
Thank you.










share|improve this question













I have been looking for some time on ways to open a second form from another already shown form.



This is some piece of code that works:



frmSecond second = new frmSecond();
this.Hide();
second.ShowDialog();
this.Close();


What it does basically is to Hide() the currently opened form, then it opens another form (the ShowDialog() method). It will only Close() the currently hidden form when the form you have just created is closed.



The problem here is: this way of doing it creates an immense thread of forms. If I need to go from frmSecond to frmThird, it will maintain the first form and the frmSecond being executed in the background, while only showing the frmThird.



Then, as the frmThird is open, if I need to get back to the first form, I would use some code like:



frmFirst first = new frmFirst();
this.Hide();
first.ShowDialog();
this.Close();


And it would create another frmFirst! Then we would have three forms being executed in the background (the first frmFirst, frmSecond, and frmThird).



This method works, but uses an increasingly amount of processing memory, which may be prejudicial for any kind of project.



Is there any alternative or add up to correct this problem?



If anything is unclear, please don't bother in letting me know.
Thank you.







c# .net winforms






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 18 at 23:46









Estêvão Rolim

32




32












  • you could use using(Form f = new Form()) { f.ShowDialog(); } it will automatically dispose of form when closed
    – Aleksa Ristic
    Nov 19 at 0:02






  • 1




    Usually a winforms application will have a main form and open subsequent forms, Generally normal applications don't try to minimise the previous form, as the next child form is related to the previous one
    – TheGeneral
    Nov 19 at 0:03










  • Secondly, when you are finished with a child form you close it, if its modal, you usually have it in a using statement, control goes back to your previous (non hidden form) and you dont have to create a new form
    – TheGeneral
    Nov 19 at 0:07






  • 1




    Hide() does not hide anything from the machine, only from the user's eyes. Look here for a possible way to get ahead.
    – Hans Passant
    Nov 19 at 0:50










  • The thing is: the form is already disposed (and closed) when the user come back from it, but I have a kind of complex menu structure, so it's not only one parent form and a bunch of children. What I'm trying to do is creating a menu, that redirects to a Sign in form that will, if the user and password are correct, show the user a dashboard, that will have a lot of other options. So there are dozens of parent/children relations, that create and need this enormous amount of processing memory.
    – Estêvão Rolim
    Nov 19 at 3:00


















  • you could use using(Form f = new Form()) { f.ShowDialog(); } it will automatically dispose of form when closed
    – Aleksa Ristic
    Nov 19 at 0:02






  • 1




    Usually a winforms application will have a main form and open subsequent forms, Generally normal applications don't try to minimise the previous form, as the next child form is related to the previous one
    – TheGeneral
    Nov 19 at 0:03










  • Secondly, when you are finished with a child form you close it, if its modal, you usually have it in a using statement, control goes back to your previous (non hidden form) and you dont have to create a new form
    – TheGeneral
    Nov 19 at 0:07






  • 1




    Hide() does not hide anything from the machine, only from the user's eyes. Look here for a possible way to get ahead.
    – Hans Passant
    Nov 19 at 0:50










  • The thing is: the form is already disposed (and closed) when the user come back from it, but I have a kind of complex menu structure, so it's not only one parent form and a bunch of children. What I'm trying to do is creating a menu, that redirects to a Sign in form that will, if the user and password are correct, show the user a dashboard, that will have a lot of other options. So there are dozens of parent/children relations, that create and need this enormous amount of processing memory.
    – Estêvão Rolim
    Nov 19 at 3:00
















you could use using(Form f = new Form()) { f.ShowDialog(); } it will automatically dispose of form when closed
– Aleksa Ristic
Nov 19 at 0:02




you could use using(Form f = new Form()) { f.ShowDialog(); } it will automatically dispose of form when closed
– Aleksa Ristic
Nov 19 at 0:02




1




1




Usually a winforms application will have a main form and open subsequent forms, Generally normal applications don't try to minimise the previous form, as the next child form is related to the previous one
– TheGeneral
Nov 19 at 0:03




Usually a winforms application will have a main form and open subsequent forms, Generally normal applications don't try to minimise the previous form, as the next child form is related to the previous one
– TheGeneral
Nov 19 at 0:03












Secondly, when you are finished with a child form you close it, if its modal, you usually have it in a using statement, control goes back to your previous (non hidden form) and you dont have to create a new form
– TheGeneral
Nov 19 at 0:07




Secondly, when you are finished with a child form you close it, if its modal, you usually have it in a using statement, control goes back to your previous (non hidden form) and you dont have to create a new form
– TheGeneral
Nov 19 at 0:07




1




1




Hide() does not hide anything from the machine, only from the user's eyes. Look here for a possible way to get ahead.
– Hans Passant
Nov 19 at 0:50




Hide() does not hide anything from the machine, only from the user's eyes. Look here for a possible way to get ahead.
– Hans Passant
Nov 19 at 0:50












The thing is: the form is already disposed (and closed) when the user come back from it, but I have a kind of complex menu structure, so it's not only one parent form and a bunch of children. What I'm trying to do is creating a menu, that redirects to a Sign in form that will, if the user and password are correct, show the user a dashboard, that will have a lot of other options. So there are dozens of parent/children relations, that create and need this enormous amount of processing memory.
– Estêvão Rolim
Nov 19 at 3:00




The thing is: the form is already disposed (and closed) when the user come back from it, but I have a kind of complex menu structure, so it's not only one parent form and a bunch of children. What I'm trying to do is creating a menu, that redirects to a Sign in form that will, if the user and password are correct, show the user a dashboard, that will have a lot of other options. So there are dozens of parent/children relations, that create and need this enormous amount of processing memory.
– Estêvão Rolim
Nov 19 at 3:00












3 Answers
3






active

oldest

votes

















up vote
2
down vote



accepted










If you want to get access to already created forms try using the static Application.OpenForms property. It contains a list of all of the forms currently open in your application. Documentation is here.



As an example, if you always want to keep frmFirst open and then navigate back to it when you close one of your other forms you can do this:



frmFirst existing = Application.OpenForms.OfType<frmFirst>().FirstOrDefault();
if (existing != null)
existing.Show();


You would need to remove your this.Close() calls for this to work.






share|improve this answer





















  • This is awesome, but I'm not sure I'll be able to use it to every form opened in the project. In the main menu, the user will be redirected to a login form, where, if the user and the password is correct, he/she will be redirected to a dashboard, where he/she will have access to a bunch of other options (and forms). So this method is probably more practical if there are forms only with simple constructors, in this project's case, there are dozens of forms with a lot of different arguments. Guess I'll just need a lot of memory anyway, hehe
    – Estêvão Rolim
    Nov 19 at 3:06










  • Perfect, Thank you very much!
    – Estêvão Rolim
    Nov 21 at 19:43


















up vote
0
down vote













To free the memory you need to dispose the form using the Dispose method when it is no longer needed.






share|improve this answer




























    up vote
    0
    down vote













    Seems like you have a desing error here




    Then, as the frmThird is open, if I need to get back to the first
    form, I would use some code like:



    frmFirst first = new frmFirst();
    this.Hide();
    first.ShowDialog();
    this.Close();



    This will leave the original created frmFirst in memory, not visible, doing nothing but eating memory.

    If you know you want to get back to frmFirst that was created before, why not just do this :



    frmFirst.Show();


    and save you lots of memory.



    You have 2 choices actually




    1. Hide forms and reactivate them when needed

    2. Close and dispose forms, and recreate them when needed


    What you are doing is creating each form over and over again, without getting rid of the prior created forms. Hence you need lots of memory for hidden forms...






    share|improve this answer























    • Oh, ok! So I'll basically need to use the code Handbag Crab showed up there.
      – Estêvão Rolim
      Nov 20 at 0:25










    • this: frmFirst existing = Application.OpenForms.OfType<frmFirst>().FirstOrDefault();
      – Estêvão Rolim
      Nov 20 at 0:25










    • Yes, that is a perfect way to do it. Use the answer from @handbag-crap
      – GuidoG
      Nov 20 at 7:35












    • Thank you very much, GuidoG
      – Estêvão Rolim
      Nov 21 at 19:42











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53366610%2fc-sharp-open-a-form-from-another-form-too-much-processing-memory%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote



    accepted










    If you want to get access to already created forms try using the static Application.OpenForms property. It contains a list of all of the forms currently open in your application. Documentation is here.



    As an example, if you always want to keep frmFirst open and then navigate back to it when you close one of your other forms you can do this:



    frmFirst existing = Application.OpenForms.OfType<frmFirst>().FirstOrDefault();
    if (existing != null)
    existing.Show();


    You would need to remove your this.Close() calls for this to work.






    share|improve this answer





















    • This is awesome, but I'm not sure I'll be able to use it to every form opened in the project. In the main menu, the user will be redirected to a login form, where, if the user and the password is correct, he/she will be redirected to a dashboard, where he/she will have access to a bunch of other options (and forms). So this method is probably more practical if there are forms only with simple constructors, in this project's case, there are dozens of forms with a lot of different arguments. Guess I'll just need a lot of memory anyway, hehe
      – Estêvão Rolim
      Nov 19 at 3:06










    • Perfect, Thank you very much!
      – Estêvão Rolim
      Nov 21 at 19:43















    up vote
    2
    down vote



    accepted










    If you want to get access to already created forms try using the static Application.OpenForms property. It contains a list of all of the forms currently open in your application. Documentation is here.



    As an example, if you always want to keep frmFirst open and then navigate back to it when you close one of your other forms you can do this:



    frmFirst existing = Application.OpenForms.OfType<frmFirst>().FirstOrDefault();
    if (existing != null)
    existing.Show();


    You would need to remove your this.Close() calls for this to work.






    share|improve this answer





















    • This is awesome, but I'm not sure I'll be able to use it to every form opened in the project. In the main menu, the user will be redirected to a login form, where, if the user and the password is correct, he/she will be redirected to a dashboard, where he/she will have access to a bunch of other options (and forms). So this method is probably more practical if there are forms only with simple constructors, in this project's case, there are dozens of forms with a lot of different arguments. Guess I'll just need a lot of memory anyway, hehe
      – Estêvão Rolim
      Nov 19 at 3:06










    • Perfect, Thank you very much!
      – Estêvão Rolim
      Nov 21 at 19:43













    up vote
    2
    down vote



    accepted







    up vote
    2
    down vote



    accepted






    If you want to get access to already created forms try using the static Application.OpenForms property. It contains a list of all of the forms currently open in your application. Documentation is here.



    As an example, if you always want to keep frmFirst open and then navigate back to it when you close one of your other forms you can do this:



    frmFirst existing = Application.OpenForms.OfType<frmFirst>().FirstOrDefault();
    if (existing != null)
    existing.Show();


    You would need to remove your this.Close() calls for this to work.






    share|improve this answer












    If you want to get access to already created forms try using the static Application.OpenForms property. It contains a list of all of the forms currently open in your application. Documentation is here.



    As an example, if you always want to keep frmFirst open and then navigate back to it when you close one of your other forms you can do this:



    frmFirst existing = Application.OpenForms.OfType<frmFirst>().FirstOrDefault();
    if (existing != null)
    existing.Show();


    You would need to remove your this.Close() calls for this to work.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 19 at 0:01









    Handbag Crab

    1,247212




    1,247212












    • This is awesome, but I'm not sure I'll be able to use it to every form opened in the project. In the main menu, the user will be redirected to a login form, where, if the user and the password is correct, he/she will be redirected to a dashboard, where he/she will have access to a bunch of other options (and forms). So this method is probably more practical if there are forms only with simple constructors, in this project's case, there are dozens of forms with a lot of different arguments. Guess I'll just need a lot of memory anyway, hehe
      – Estêvão Rolim
      Nov 19 at 3:06










    • Perfect, Thank you very much!
      – Estêvão Rolim
      Nov 21 at 19:43


















    • This is awesome, but I'm not sure I'll be able to use it to every form opened in the project. In the main menu, the user will be redirected to a login form, where, if the user and the password is correct, he/she will be redirected to a dashboard, where he/she will have access to a bunch of other options (and forms). So this method is probably more practical if there are forms only with simple constructors, in this project's case, there are dozens of forms with a lot of different arguments. Guess I'll just need a lot of memory anyway, hehe
      – Estêvão Rolim
      Nov 19 at 3:06










    • Perfect, Thank you very much!
      – Estêvão Rolim
      Nov 21 at 19:43
















    This is awesome, but I'm not sure I'll be able to use it to every form opened in the project. In the main menu, the user will be redirected to a login form, where, if the user and the password is correct, he/she will be redirected to a dashboard, where he/she will have access to a bunch of other options (and forms). So this method is probably more practical if there are forms only with simple constructors, in this project's case, there are dozens of forms with a lot of different arguments. Guess I'll just need a lot of memory anyway, hehe
    – Estêvão Rolim
    Nov 19 at 3:06




    This is awesome, but I'm not sure I'll be able to use it to every form opened in the project. In the main menu, the user will be redirected to a login form, where, if the user and the password is correct, he/she will be redirected to a dashboard, where he/she will have access to a bunch of other options (and forms). So this method is probably more practical if there are forms only with simple constructors, in this project's case, there are dozens of forms with a lot of different arguments. Guess I'll just need a lot of memory anyway, hehe
    – Estêvão Rolim
    Nov 19 at 3:06












    Perfect, Thank you very much!
    – Estêvão Rolim
    Nov 21 at 19:43




    Perfect, Thank you very much!
    – Estêvão Rolim
    Nov 21 at 19:43












    up vote
    0
    down vote













    To free the memory you need to dispose the form using the Dispose method when it is no longer needed.






    share|improve this answer

























      up vote
      0
      down vote













      To free the memory you need to dispose the form using the Dispose method when it is no longer needed.






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        To free the memory you need to dispose the form using the Dispose method when it is no longer needed.






        share|improve this answer












        To free the memory you need to dispose the form using the Dispose method when it is no longer needed.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 at 0:40









        Ivan Ičin

        3,43642342




        3,43642342






















            up vote
            0
            down vote













            Seems like you have a desing error here




            Then, as the frmThird is open, if I need to get back to the first
            form, I would use some code like:



            frmFirst first = new frmFirst();
            this.Hide();
            first.ShowDialog();
            this.Close();



            This will leave the original created frmFirst in memory, not visible, doing nothing but eating memory.

            If you know you want to get back to frmFirst that was created before, why not just do this :



            frmFirst.Show();


            and save you lots of memory.



            You have 2 choices actually




            1. Hide forms and reactivate them when needed

            2. Close and dispose forms, and recreate them when needed


            What you are doing is creating each form over and over again, without getting rid of the prior created forms. Hence you need lots of memory for hidden forms...






            share|improve this answer























            • Oh, ok! So I'll basically need to use the code Handbag Crab showed up there.
              – Estêvão Rolim
              Nov 20 at 0:25










            • this: frmFirst existing = Application.OpenForms.OfType<frmFirst>().FirstOrDefault();
              – Estêvão Rolim
              Nov 20 at 0:25










            • Yes, that is a perfect way to do it. Use the answer from @handbag-crap
              – GuidoG
              Nov 20 at 7:35












            • Thank you very much, GuidoG
              – Estêvão Rolim
              Nov 21 at 19:42















            up vote
            0
            down vote













            Seems like you have a desing error here




            Then, as the frmThird is open, if I need to get back to the first
            form, I would use some code like:



            frmFirst first = new frmFirst();
            this.Hide();
            first.ShowDialog();
            this.Close();



            This will leave the original created frmFirst in memory, not visible, doing nothing but eating memory.

            If you know you want to get back to frmFirst that was created before, why not just do this :



            frmFirst.Show();


            and save you lots of memory.



            You have 2 choices actually




            1. Hide forms and reactivate them when needed

            2. Close and dispose forms, and recreate them when needed


            What you are doing is creating each form over and over again, without getting rid of the prior created forms. Hence you need lots of memory for hidden forms...






            share|improve this answer























            • Oh, ok! So I'll basically need to use the code Handbag Crab showed up there.
              – Estêvão Rolim
              Nov 20 at 0:25










            • this: frmFirst existing = Application.OpenForms.OfType<frmFirst>().FirstOrDefault();
              – Estêvão Rolim
              Nov 20 at 0:25










            • Yes, that is a perfect way to do it. Use the answer from @handbag-crap
              – GuidoG
              Nov 20 at 7:35












            • Thank you very much, GuidoG
              – Estêvão Rolim
              Nov 21 at 19:42













            up vote
            0
            down vote










            up vote
            0
            down vote









            Seems like you have a desing error here




            Then, as the frmThird is open, if I need to get back to the first
            form, I would use some code like:



            frmFirst first = new frmFirst();
            this.Hide();
            first.ShowDialog();
            this.Close();



            This will leave the original created frmFirst in memory, not visible, doing nothing but eating memory.

            If you know you want to get back to frmFirst that was created before, why not just do this :



            frmFirst.Show();


            and save you lots of memory.



            You have 2 choices actually




            1. Hide forms and reactivate them when needed

            2. Close and dispose forms, and recreate them when needed


            What you are doing is creating each form over and over again, without getting rid of the prior created forms. Hence you need lots of memory for hidden forms...






            share|improve this answer














            Seems like you have a desing error here




            Then, as the frmThird is open, if I need to get back to the first
            form, I would use some code like:



            frmFirst first = new frmFirst();
            this.Hide();
            first.ShowDialog();
            this.Close();



            This will leave the original created frmFirst in memory, not visible, doing nothing but eating memory.

            If you know you want to get back to frmFirst that was created before, why not just do this :



            frmFirst.Show();


            and save you lots of memory.



            You have 2 choices actually




            1. Hide forms and reactivate them when needed

            2. Close and dispose forms, and recreate them when needed


            What you are doing is creating each form over and over again, without getting rid of the prior created forms. Hence you need lots of memory for hidden forms...







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 19 at 14:05

























            answered Nov 19 at 13:57









            GuidoG

            5,16331743




            5,16331743












            • Oh, ok! So I'll basically need to use the code Handbag Crab showed up there.
              – Estêvão Rolim
              Nov 20 at 0:25










            • this: frmFirst existing = Application.OpenForms.OfType<frmFirst>().FirstOrDefault();
              – Estêvão Rolim
              Nov 20 at 0:25










            • Yes, that is a perfect way to do it. Use the answer from @handbag-crap
              – GuidoG
              Nov 20 at 7:35












            • Thank you very much, GuidoG
              – Estêvão Rolim
              Nov 21 at 19:42


















            • Oh, ok! So I'll basically need to use the code Handbag Crab showed up there.
              – Estêvão Rolim
              Nov 20 at 0:25










            • this: frmFirst existing = Application.OpenForms.OfType<frmFirst>().FirstOrDefault();
              – Estêvão Rolim
              Nov 20 at 0:25










            • Yes, that is a perfect way to do it. Use the answer from @handbag-crap
              – GuidoG
              Nov 20 at 7:35












            • Thank you very much, GuidoG
              – Estêvão Rolim
              Nov 21 at 19:42
















            Oh, ok! So I'll basically need to use the code Handbag Crab showed up there.
            – Estêvão Rolim
            Nov 20 at 0:25




            Oh, ok! So I'll basically need to use the code Handbag Crab showed up there.
            – Estêvão Rolim
            Nov 20 at 0:25












            this: frmFirst existing = Application.OpenForms.OfType<frmFirst>().FirstOrDefault();
            – Estêvão Rolim
            Nov 20 at 0:25




            this: frmFirst existing = Application.OpenForms.OfType<frmFirst>().FirstOrDefault();
            – Estêvão Rolim
            Nov 20 at 0:25












            Yes, that is a perfect way to do it. Use the answer from @handbag-crap
            – GuidoG
            Nov 20 at 7:35






            Yes, that is a perfect way to do it. Use the answer from @handbag-crap
            – GuidoG
            Nov 20 at 7:35














            Thank you very much, GuidoG
            – Estêvão Rolim
            Nov 21 at 19:42




            Thank you very much, GuidoG
            – Estêvão Rolim
            Nov 21 at 19:42


















             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53366610%2fc-sharp-open-a-form-from-another-form-too-much-processing-memory%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown