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. How can I make one Form show another?

How can I make one Form show another?

Scheduled Pinned Locked Moved C#
questionhelp
10 Posts 3 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.
  • A Offline
    A Offline
    Anonymous
    wrote on last edited by
    #1

    Hello all, Here's my problem. I have a Windows Form with some controls on it. When the user clicks a button, I want a second form to appear, and the first one to be hidden. I've drawn up both forms, so how can I make Form1 display Form2? Thanks!

    J 1 Reply Last reply
    0
    • A Anonymous

      Hello all, Here's my problem. I have a Windows Form with some controls on it. When the user clicks a button, I want a second form to appear, and the first one to be hidden. I've drawn up both forms, so how can I make Form1 display Form2? Thanks!

      J Offline
      J Offline
      J Dunlap
      wrote on last edited by
      #2

      Form2 frm=new Form2();
      frm.Show();
      this.Hide(); //or this.Close();

      Anonymous? :(

      "Blessed are the peacemakers, for they shall be called sons of God." - Jesus
      "You must be the change you wish to see in the world." - Mahatma Gandhi

      A 1 Reply Last reply
      0
      • J J Dunlap

        Form2 frm=new Form2();
        frm.Show();
        this.Hide(); //or this.Close();

        Anonymous? :(

        "Blessed are the peacemakers, for they shall be called sons of God." - Jesus
        "You must be the change you wish to see in the world." - Mahatma Gandhi

        A Offline
        A Offline
        Anonymous
        wrote on last edited by
        #3

        Thanks. Sorry for posting Anon. I forgot my password and I'm on a coding spree, so don't want to hunt it down. :)

        J 1 Reply Last reply
        0
        • A Anonymous

          Thanks. Sorry for posting Anon. I forgot my password and I'm on a coding spree, so don't want to hunt it down. :)

          J Offline
          J Offline
          J Dunlap
          wrote on last edited by
          #4

          Ok. :)

          "Blessed are the peacemakers, for they shall be called sons of God." - Jesus
          "You must be the change you wish to see in the world." - Mahatma Gandhi

          A 1 Reply Last reply
          0
          • J J Dunlap

            Ok. :)

            "Blessed are the peacemakers, for they shall be called sons of God." - Jesus
            "You must be the change you wish to see in the world." - Mahatma Gandhi

            A Offline
            A Offline
            Anonymous
            wrote on last edited by
            #5

            I have one other detail I hope you won't mind answering. When the second form is closed via the close button (next to the minimize and maximize buttons in the upper right) the second form is disposed. However, Form1 remains, albeit hidden. So the program doesn't terminate. I've tried calling Form1.Dispose() after Form2 is shown but that just quits the program. Any thoughts on how I can make the program quit when the user clicks the X?

            J 1 Reply Last reply
            0
            • A Anonymous

              I have one other detail I hope you won't mind answering. When the second form is closed via the close button (next to the minimize and maximize buttons in the upper right) the second form is disposed. However, Form1 remains, albeit hidden. So the program doesn't terminate. I've tried calling Form1.Dispose() after Form2 is shown but that just quits the program. Any thoughts on how I can make the program quit when the user clicks the X?

              J Offline
              J Offline
              J Dunlap
              wrote on last edited by
              #6

              You'll want to do this.Close(), not this.Hide. But that will release the ref to Form2, which may make it close, too. How about

              Application.Run(new Form2());
              this.Close();

              "Blessed are the peacemakers, for they shall be called sons of God." - Jesus
              "You must be the change you wish to see in the world." - Mahatma Gandhi

              A 1 Reply Last reply
              0
              • J J Dunlap

                You'll want to do this.Close(), not this.Hide. But that will release the ref to Form2, which may make it close, too. How about

                Application.Run(new Form2());
                this.Close();

                "Blessed are the peacemakers, for they shall be called sons of God." - Jesus
                "You must be the change you wish to see in the world." - Mahatma Gandhi

                A Offline
                A Offline
                Anonymous
                wrote on last edited by
                #7

                I get this error: An unhandled exception of type 'System.InvalidOperationException' occurred in system.windows.forms.dll Additional information: It is invalid to start a second message loop on a single thread. Use Application.RunDialog or Form.ShowDialog instead. Trying Application.RunDialog gives me this error: C:\Documents and Settings\...\Form1.cs(402): 'System.Windows.Forms.Application.RunDialog(System.Windows.Forms.Form)' is inaccessible due to its protection level Not sure exactly what that means, but any other thoughts? (Oh, and this.Close() on Form1 does quit the app.)

                F 1 Reply Last reply
                0
                • A Anonymous

                  I get this error: An unhandled exception of type 'System.InvalidOperationException' occurred in system.windows.forms.dll Additional information: It is invalid to start a second message loop on a single thread. Use Application.RunDialog or Form.ShowDialog instead. Trying Application.RunDialog gives me this error: C:\Documents and Settings\...\Form1.cs(402): 'System.Windows.Forms.Application.RunDialog(System.Windows.Forms.Form)' is inaccessible due to its protection level Not sure exactly what that means, but any other thoughts? (Oh, and this.Close() on Form1 does quit the app.)

                  F Offline
                  F Offline
                  frogb0x
                  wrote on last edited by
                  #8

                  I went into the Closed event for Form2 and just put in this line, which works: Application.Exit(); Still, I'd like to know if there's a way to do it without having to add that. PS: Since I'm posting so much, I thought I should hunt down my p/w and login! ;)

                  J 1 Reply Last reply
                  0
                  • F frogb0x

                    I went into the Closed event for Form2 and just put in this line, which works: Application.Exit(); Still, I'd like to know if there's a way to do it without having to add that. PS: Since I'm posting so much, I thought I should hunt down my p/w and login! ;)

                    J Offline
                    J Offline
                    J Dunlap
                    wrote on last edited by
                    #9

                    That's the way I was going to suggest doing it. :) frogb0x wrote: Since I'm posting so much, I thought I should hunt down my p/w and login! :-D

                    "Blessed are the peacemakers, for they shall be called sons of God." - Jesus
                    "You must be the change you wish to see in the world." - Mahatma Gandhi

                    F 1 Reply Last reply
                    0
                    • J J Dunlap

                      That's the way I was going to suggest doing it. :) frogb0x wrote: Since I'm posting so much, I thought I should hunt down my p/w and login! :-D

                      "Blessed are the peacemakers, for they shall be called sons of God." - Jesus
                      "You must be the change you wish to see in the world." - Mahatma Gandhi

                      F Offline
                      F Offline
                      frogb0x
                      wrote on last edited by
                      #10

                      So it seems that's the only way to do it, eh? Cool. Thanks for all your help! :)

                      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