Close all forms but Main

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ryan

    #1

    Close all forms but Main

    My application has a main form from which the user can use the Menu to open
    other forms. Each form opens in a separate window while the main form
    remains open all the time. Is their any code I can place in Form_Load for
    all my other forms (besides Main) that will close all other open forms (if
    any exist) except for the Main form? Something like For Each Form in
    Windows.Forms?? What's the code?'

    Thanks,
    Ryan


  • tlkerns

    #2
    RE: Close all forms but Main

    Check out the Application.Ope nForms Property.

    Tony

    "Ryan" wrote:
    [color=blue]
    > My application has a main form from which the user can use the Menu to open
    > other forms. Each form opens in a separate window while the main form
    > remains open all the time. Is their any code I can place in Form_Load for
    > all my other forms (besides Main) that will close all other open forms (if
    > any exist) except for the Main form? Something like For Each Form in
    > Windows.Forms?? What's the code?'
    >
    > Thanks,
    > Ryan
    >
    >
    >[/color]

    Comment

    • Kevin Yu [MSFT]

      #3
      RE: Close all forms but Main

      Hi Ryan,

      Yes, you can go through the Application.Ope nForms collection and close all
      of them, except the main form. You can check the Form.Text property for the
      main form.

      Kevin Yu
      Microsoft Online Community Support

      =============== =============== =============== =============== =============== =
      =============== ===========
      When responding to posts, please "Reply to Group" via your newsreader so
      that others may learn and benefit from your issue.
      =============== =============== =============== =============== =============== =
      =============== ===========

      (This posting is provided "AS IS", with no warranties, and confers no
      rights.)

      Comment

      • Ryan

        #4
        Re: Close all forms but Main

        I tried this code and I get an error everytime a Form closes because the
        objects in My.Application. OpenForms change within the for loop.

        Private Sub Application_Loa d(ByVal sender As System.Object, ByVal e As
        System.EventArg s) Handles MyBase.Load

        For Each f As Form In My.Application. OpenForms

        If f.Name <"MainForm" Then

        f.Close()

        End If

        Next

        End Sub



        "Kevin Yu [MSFT]" <v-kevy@online.mic rosoft.comwrote in message
        news:vT5P1tjnGH A.4612@TK2MSFTN GXA01.phx.gbl.. .
        Hi Ryan,
        >
        Yes, you can go through the Application.Ope nForms collection and close all
        of them, except the main form. You can check the Form.Text property for
        the
        main form.
        >
        Kevin Yu
        Microsoft Online Community Support
        >
        =============== =============== =============== =============== =============== =
        =============== ===========
        When responding to posts, please "Reply to Group" via your newsreader so
        that others may learn and benefit from your issue.
        =============== =============== =============== =============== =============== =
        =============== ===========
        >
        (This posting is provided "AS IS", with no warranties, and confers no
        rights.)
        >

        Comment

        • Kevin Yu [MSFT]

          #5
          Re: Close all forms but Main

          Hi Ryan,

          You can take advantage of GoTo and Try/Catch block. Not very graceful, but
          it does work. :-)

          Try
          a:
          For Each f As Form In My.Application. OpenForms

          If f.Name <"Form1" Then

          f.Close()

          End If

          Next

          Catch
          GoTo a
          End Try

          Kevin Yu
          Microsoft Online Community Support

          =============== =============== =============== =============== =============== =
          =============== ===========
          When responding to posts, please "Reply to Group" via your newsreader so
          that others may learn and benefit from your issue.
          =============== =============== =============== =============== =============== =
          =============== ===========

          (This posting is provided "AS IS", with no warranties, and confers no
          rights.)

          Comment

          • Michael D. Ober

            #6
            Re: Close all forms but Main

            Dump the goto:

            dim Success as Boolean
            do
            success = true
            try
            for each f as form in my.application. openforms
            if f.name <"Form1" then f.close
            next f
            catch ex as exception
            success = false
            end try
            loop until success

            With structured exception handling, Goto should generate a compiler warning
            "Warning: Speghetti Code using Goto". Since you can jump an arbritrary
            number of levels up via try ... catch blocks, there is no longer any need in
            the VB language for Goto.

            Mike Ober.

            "Kevin Yu [MSFT]" <v-kevy@online.mic rosoft.comwrote in message
            news:N8sDvOJpGH A.2024@TK2MSFTN GXA01.phx.gbl.. .
            Hi Ryan,
            >
            You can take advantage of GoTo and Try/Catch block. Not very graceful, but
            it does work. :-)
            >
            Try
            a:
            For Each f As Form In My.Application. OpenForms
            >
            If f.Name <"Form1" Then
            >
            f.Close()
            >
            End If
            >
            Next
            >
            Catch
            GoTo a
            End Try
            >
            Kevin Yu
            Microsoft Online Community Support
            >
            >
            =============== =============== =============== =============== =============== =
            =============== ===========
            When responding to posts, please "Reply to Group" via your newsreader so
            that others may learn and benefit from your issue.
            >
            =============== =============== =============== =============== =============== =
            =============== ===========
            >
            (This posting is provided "AS IS", with no warranties, and confers no
            rights.)
            >
            >


            Comment

            • Kevin Yu [MSFT]

              #7
              Re: Close all forms but Main

              Thanks, Michael! Looks better.

              Kevin Yu
              Microsoft Online Community Support

              =============== =============== =============== =============== =============== =
              =============== ===========
              When responding to posts, please "Reply to Group" via your newsreader so
              that others may learn and benefit from your issue.
              =============== =============== =============== =============== =============== =
              =============== ===========

              (This posting is provided "AS IS", with no warranties, and confers no
              rights.)

              Comment

              Working...