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. Visual Basic
  4. Close all forms but one

Close all forms but one

Scheduled Pinned Locked Moved Visual Basic
csharpquestion
12 Posts 4 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.
  • H harveyhanson

    Is there a way in VB.NET 2005 to close all forms (or hide) except the one that is being loaded, without typing a load of code to hide each form? Cheers

    P Offline
    P Offline
    Paul Conrad
    wrote on last edited by
    #2

    I don't remember off the top of my head, but I think you can loop through all of the Form objects and hide all but the one you want to keep open. Hope this starts you off on the right foot...

    1 Reply Last reply
    0
    • H harveyhanson

      Is there a way in VB.NET 2005 to close all forms (or hide) except the one that is being loaded, without typing a load of code to hide each form? Cheers

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #3

      The Application object has a property that lists all open forms, you can iterate over that.

      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

      H 1 Reply Last reply
      0
      • C Christian Graus

        The Application object has a property that lists all open forms, you can iterate over that.

        Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

        H Offline
        H Offline
        harveyhanson
        wrote on last edited by
        #4

        What do you mean, application.exit.... something??? Im not a wiz at vb by the way, just a beginner!

        D 1 Reply Last reply
        0
        • H harveyhanson

          What do you mean, application.exit.... something??? Im not a wiz at vb by the way, just a beginner!

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #5

          harveyhanson wrote:

          Im not a wiz at vb by the way, just a beginner!

          This makes me question what you're really trying to do with this. The Application object has a collection called OpenForms. All you need to do is iterate over this collection and call Hide on each form, taking care not to hide the form you want to show.

          For Each f As Form in Application.OpenForms
              ' Make sure we're not hiding the form this code is on...
              If f Is Not Me Then
                  f.Hide
              Next
          Next
          

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
               2006, 2007

          H P 3 Replies Last reply
          0
          • D Dave Kreskowiak

            harveyhanson wrote:

            Im not a wiz at vb by the way, just a beginner!

            This makes me question what you're really trying to do with this. The Application object has a collection called OpenForms. All you need to do is iterate over this collection and call Hide on each form, taking care not to hide the form you want to show.

            For Each f As Form in Application.OpenForms
                ' Make sure we're not hiding the form this code is on...
                If f Is Not Me Then
                    f.Hide
                Next
            Next
            

            A guide to posting questions on CodeProject[^]
            Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                 2006, 2007

            H Offline
            H Offline
            harveyhanson
            wrote on last edited by
            #6

            The aim is to make a goodbye screen, but at the mo it is just over the top of the current form, whereas i want that to be the only form shown! I will have a go with this statement

            D 1 Reply Last reply
            0
            • D Dave Kreskowiak

              harveyhanson wrote:

              Im not a wiz at vb by the way, just a beginner!

              This makes me question what you're really trying to do with this. The Application object has a collection called OpenForms. All you need to do is iterate over this collection and call Hide on each form, taking care not to hide the form you want to show.

              For Each f As Form in Application.OpenForms
                  ' Make sure we're not hiding the form this code is on...
                  If f Is Not Me Then
                      f.Hide
                  Next
              Next
              

              A guide to posting questions on CodeProject[^]
              Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                   2006, 2007

              P Offline
              P Offline
              Paul Conrad
              wrote on last edited by
              #7

              Dave Kreskowiak wrote:

              For Each f As Form in Application.OpenForms ' Make sure we're not hiding the form this code is on... If f Is Not Me Then f.Hide Next Next

              That was what I was thinking of yesterday when I replied in the earlier post...

              1 Reply Last reply
              0
              • H harveyhanson

                The aim is to make a goodbye screen, but at the mo it is just over the top of the current form, whereas i want that to be the only form shown! I will have a go with this statement

                D Offline
                D Offline
                Dave Kreskowiak
                wrote on last edited by
                #8

                Then that's what you do. After that, you can wait a little bit, then call Application.Exit.

                A guide to posting questions on CodeProject[^]
                Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                     2006, 2007

                1 Reply Last reply
                0
                • D Dave Kreskowiak

                  harveyhanson wrote:

                  Im not a wiz at vb by the way, just a beginner!

                  This makes me question what you're really trying to do with this. The Application object has a collection called OpenForms. All you need to do is iterate over this collection and call Hide on each form, taking care not to hide the form you want to show.

                  For Each f As Form in Application.OpenForms
                      ' Make sure we're not hiding the form this code is on...
                      If f Is Not Me Then
                          f.Hide
                      Next
                  Next
                  

                  A guide to posting questions on CodeProject[^]
                  Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                       2006, 2007

                  H Offline
                  H Offline
                  harveyhanson
                  wrote on last edited by
                  #9

                  is this the exact code i should use? I have tried it and i get how it works, but i am getting an error on "not me" bit, and why is there 2 nexts, wouldnt one just create the loop?!

                  D 1 Reply Last reply
                  0
                  • H harveyhanson

                    is this the exact code i should use? I have tried it and i get how it works, but i am getting an error on "not me" bit, and why is there 2 nexts, wouldnt one just create the loop?!

                    D Offline
                    D Offline
                    Dave Kreskowiak
                    wrote on last edited by
                    #10

                    That first Next should be an "End If" actually. You should NEVER just copy and paste code. Try and understand any examples and write your own. There's too many "Copy'n'Paste" programmers out there clogging up the forums.

                    A guide to posting questions on CodeProject[^]
                    Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                         2006, 2007

                    H 1 Reply Last reply
                    0
                    • D Dave Kreskowiak

                      That first Next should be an "End If" actually. You should NEVER just copy and paste code. Try and understand any examples and write your own. There's too many "Copy'n'Paste" programmers out there clogging up the forums.

                      A guide to posting questions on CodeProject[^]
                      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                           2006, 2007

                      H Offline
                      H Offline
                      harveyhanson
                      wrote on last edited by
                      #11

                      Dont worry, i am just trying out code really, not copying and pasting and leaving it that way. Still, what is the "not me" bit about?

                      D 1 Reply Last reply
                      0
                      • H harveyhanson

                        Dont worry, i am just trying out code really, not copying and pasting and leaving it that way. Still, what is the "not me" bit about?

                        D Offline
                        D Offline
                        Dave Kreskowiak
                        wrote on last edited by
                        #12

                        Me always referes to the current instance of an object. Since every open form ends up in the OpenForms collection, that loop has to check to see if it's going to hide the form the code is running on.

                        A guide to posting questions on CodeProject[^]
                        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                             2006, 2007

                        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