Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. closing the main form after loading another form

closing the main form after loading another form

Scheduled Pinned Locked Moved C#
help
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    killermoses
    wrote on last edited by
    #1

    I need to go to another form in my project. I need that the main form will close when I press a button to go to the second form. I can't use Application.Exit because than the second form closes too. please help!!!!

    J O C 3 Replies Last reply
    0
    • K killermoses

      I need to go to another form in my project. I need that the main form will close when I press a button to go to the second form. I can't use Application.Exit because than the second form closes too. please help!!!!

      J Offline
      J Offline
      John Kuhn
      wrote on last edited by
      #2

      Can you temporarily hide the main form? What a piece of work is man, how noble in reason, how infinite in faculties, in form and moving how express and admirable . . . and yet to me, what is this quintessence of dust? -- Hamlet, Act II, Scene ii.

      1 Reply Last reply
      0
      • K killermoses

        I need to go to another form in my project. I need that the main form will close when I press a button to go to the second form. I can't use Application.Exit because than the second form closes too. please help!!!!

        O Offline
        O Offline
        obelisk29
        wrote on last edited by
        #3

        Every form you create acts just like a class would so you could just do.

        MySecondForm form = new MySecondForm();
        form.Show();
        this.Dispose();

        The 'this' keyword refers to the object you're form is derived from including all of the custom objects and functions you have put in. You could also just say 'Dispose();' instead of 'this.Dispose();' but for keeping clarity it's better to use the this keyword. You could also say 'this.Hide();' but then it keeps that form in memory which when you're dealing with multiple forms this could eat up your memory pretty quickly. I just take my forms out of memory when I don't need them. ------------------ I'm naked under my clothes...

        K 1 Reply Last reply
        0
        • O obelisk29

          Every form you create acts just like a class would so you could just do.

          MySecondForm form = new MySecondForm();
          form.Show();
          this.Dispose();

          The 'this' keyword refers to the object you're form is derived from including all of the custom objects and functions you have put in. You could also just say 'Dispose();' instead of 'this.Dispose();' but for keeping clarity it's better to use the this keyword. You could also say 'this.Hide();' but then it keeps that form in memory which when you're dealing with multiple forms this could eat up your memory pretty quickly. I just take my forms out of memory when I don't need them. ------------------ I'm naked under my clothes...

          K Offline
          K Offline
          killermoses
          wrote on last edited by
          #4

          this.Dispose(); doesn't work for me. it still closes the whole application. the this.Hide works and altought I'm making a small program a slow computer is gonna run it so if you can recommend me of somthimg else that will allow me to save in memory usage please find me somthing that doesn't just hides the window but thanks anyway. Modyfied: I just realized I don't need to close the main form and that the this.Dispose works for my other forms. anyway thanks alot you've been a great help for me. see ya later

          H 1 Reply Last reply
          0
          • K killermoses

            I need to go to another form in my project. I need that the main form will close when I press a button to go to the second form. I can't use Application.Exit because than the second form closes too. please help!!!!

            C Offline
            C Offline
            Charlie Williams
            wrote on last edited by
            #5

            If you're using VS.NET, your application entry point probably looks like this:

            [STAThread]
            static void Main()
            {
            Application.Run(new Form1());
            }

            This means that 1) this call will not return until Form1 has closed 2) the application will terminate immediately after Form1 is closed. If you need to close Form1 have have the app keep plugging away, you can't rely on the VS.NET-generated code. Instead, you can write a class to handle showing the form(s) you need to show.

            //Rediculously simple example
            public class AppStarter{
            public AppStarter(){
            Form1 frm = new Form1();
            frm.Show();
            }
            [STAThread]
            public void Main(){
            Application.Run(new AppStarter());
            }
            }

            Now the application will run as long as the instance of AppStarter created in Main() is alive. You need to remember to call Application.Exit() when your last form closes or the app will never die, even if there's nothing on screen because the lifetime of the app is no longer directly related to the lifetime of the form. Charlie if(!curlies){ return; }

            1 Reply Last reply
            0
            • K killermoses

              this.Dispose(); doesn't work for me. it still closes the whole application. the this.Hide works and altought I'm making a small program a slow computer is gonna run it so if you can recommend me of somthimg else that will allow me to save in memory usage please find me somthing that doesn't just hides the window but thanks anyway. Modyfied: I just realized I don't need to close the main form and that the this.Dispose works for my other forms. anyway thanks alot you've been a great help for me. see ya later

              H Offline
              H Offline
              Heath Stewart
              wrote on last edited by
              #6

              You can't dispose the main form because that's what the message pump (which Application.Run starts) waits for. If you dispose the main form, you effectively let the message pump know that it can quit and your entry point returns. When the entry point returns, the CLR unloads your application (if there are no running background threads). See Charlie's response for a good solution (no need to repeat it). Also, you might consider a wizard-like interface, even if it has no "Back" or "Previous" button. You load one Form which adds and removes instances of container classes (like the Panel) onto its surface. This is how property sheets and wizards (which actually use the same interfaces and functions) work in Windows Explorer and other applications that use the shell-provided property sheet implementation.

              Microsoft MVP, Visual C# My Articles

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups