Closing all forms

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • kronecker@yahoo.co.uk

    Closing all forms

    I found this nifty routine that closes a form one by one every time it
    is called. However, I need to hide them instead of closing them. Is
    there a way to alter the code? I assume it has something to do with
    this line here...


    PostMessage(i, CInt(&H10), vbNullString, vbNullString)

    Thanks

    k


    Public Sub closeallforms()
    Dim objType As Type() =
    Reflection.Asse mbly.GetExecuti ngAssembly.GetT ypes()
    Dim x As Integer, i As Integer
    Try
    For x = LBound(objType) To UBound(objType)
    If Not objType(x).Name = Me.Name Then '/// make sure
    you dont unload this form yet.
    i = FindWindow(vbNu llString, objType(x).Name )
    PostMessage(i, CInt(&H10), vbNullString,
    vbNullString)
    End If
    Next
    Catch ex As Exception
    MessageBox.Show ("Oops, the following error occured:" &
    ex.Message)
    Finally
    'MyBase.Close() '/// now that all the other forms are
    closed , unload this one.
    End Try
    End Sub

  • Armin Zingler

    #2
    Re: Closing all forms

    <kronecker@yaho o.co.ukschrieb
    I found this nifty routine that closes a form one by one every time
    it is called. However, I need to hide them instead of closing them.
    Is there a way to alter the code? I assume it has something to do
    with this line here...
    >
    >
    PostMessage(i, CInt(&H10), vbNullString, vbNullString)
    I do not even quote this really _awesome_ code.

    Sorry for not answering your other question, yet. I thought that you
    should really get into some OOP basics and learn about variable scope
    before writing more lines of code.

    Well, where do I start...? Sorry, if you already know some of the
    following things:

    First, which VB version do you have? in VB 2008 (or maybe 2005) there is
    the OpenForms property (please use symbol search to find it). However,
    I've never used it because I've never needed a property like that.
    However, of course, you can use the OpenForms property.

    Second, a UI thread (user interface) has a message loop. The OS is
    message based. The message loop (see F1) is a loop that processes all
    messages put into a queue by the OS. In the Framework, the message loop
    is inside Application.Run (see object browser). There are overloaded
    versions: One has no args, one has one arg expecting a Form object. The
    one w/o args runs til the thread calls Application.Exi tThread. The other
    one does it's job til the passed Form has been closed.

    3rd, "applicatio n framework". If enabled, you can choose the startup
    Form. There is an additional option that says "til all Forms have been
    closed" (or similar). VB will create an invisible Sub Main - the UI
    thread's main procedure that every thread has - that behaves like
    instructed in the project's properties. If the "applicatio n framework"
    is disabled, you can set your own Sub Main where you can, for example,
    open a database connnection before showing any form or whatever.

    Now, which version did you choose? Why do you need to "close all
    Forms", as the subject says? Or, maybe you have an additional NotifyIcon
    in the task bar? If you have a startup Form (therefore the explanation
    above), just close that one. If you have your own Sub Main, show us how
    it looks like.

    To be able to answer your other question, please describe the scenario
    as detailled as possible. I mean, which Form opens from where and which
    one needs to close another one? Do you have an MDI From? Currently, I
    can only give the multi-purpose answer: If you need a reference to an
    object, you have to make it available. How to do it, depends on the
    situation.

    (sry for a lot to read; I always like writing about one of my favorite
    topics. ;-) )


    Armin

    Comment

    • kronecker@yahoo.co.uk

      #3
      Re: Closing all forms

      On May 3, 9:17 pm, "Armin Zingler" <az.nos...@free net.dewrote:
      <kronec...@yaho o.co.ukschrieb
      >
      I found this nifty routine that closes a form one by one every time
      it is called. However, I need to hide them instead of closing them.
      Is there a way to alter the code? I assume it has something to do
      with this line here...
      >
      PostMessage(i, CInt(&H10), vbNullString, vbNullString)
      >
      I do not even quote this really _awesome_ code.
      >
      Sorry for not answering your other question, yet. I thought that you
      should really get into some OOP basics and learn about variable scope
      before writing more lines of code.
      >
      Well, where do I start...? Sorry, if you already know some of the
      following things:
      >
      First, which VB version do you have? in VB 2008 (or maybe 2005) there is
      the OpenForms property (please use symbol search to find it). However,
      I've never used it because I've never needed a property like that.
      However, of course, you can use the OpenForms property.
      >
      Second, a UI thread (user interface) has a message loop. The OS is
      message based. The message loop (see F1) is a loop that processes all
      messages put into a queue by the OS. In the Framework, the message loop
      is inside Application.Run (see object browser). There are overloaded
      versions: One has no args, one has one arg expecting a Form object. The
      one w/o args runs til the thread calls Application.Exi tThread. The other
      one does it's job til the passed Form has been closed.
      >
      3rd, "applicatio n framework". If enabled, you can choose the startup
      Form. There is an additional option that says "til all Forms have been
      closed" (or similar). VB will create an invisible Sub Main - the UI
      thread's main procedure that every thread has - that behaves like
      instructed in the project's properties. If the "applicatio n framework"
      is disabled, you can set your own Sub Main where you can, for example,
      open a database connnection before showing any form or whatever.
      >
      Now, which version did you choose? Why do you need to "close all
      Forms", as the subject says? Or, maybe you have an additional NotifyIcon
      in the task bar? If you have a startup Form (therefore the explanation
      above), just close that one. If you have your own Sub Main, show us how
      it looks like.
      >
      To be able to answer your other question, please describe the scenario
      as detailled as possible. I mean, which Form opens from where and which
      one needs to close another one? Do you have an MDI From? Currently, I
      can only give the multi-purpose answer: If you need a reference to an
      object, you have to make it available. How to do it, depends on the
      situation.
      >
      (sry for a lot to read; I always like writing about one of my favorite
      topics. ;-) )
      >
      Armin
      I have an application which uses Windows media player on a separate
      form + other bits and pieces also on separate forms. I can close them
      all using the above code but for some reason when I close windows
      media player it will not open again unless I re-start the program.
      Therefore hiding it is a better thing to do so hiding rather than
      closing will do for me.
      However, one of my forms will not hide using the usual formx.hide( )
      method (it won't close either). So how to hide all the forms one by
      one...


      K.

      Comment

      • Armin Zingler

        #4
        Re: Closing all forms

        <kronecker@yaho o.co.ukschrieb
        >
        I have an application which uses Windows media player on a separate
        form + other bits and pieces also on separate forms. I can close
        them all using the above code but for some reason when I close
        windows media player it will not open again unless I re-start the
        program. Therefore hiding it is a better thing to do so hiding
        rather than closing will do for me.
        However, one of my forms will not hide using the usual formx.hide( )
        method (it won't close either). So how to hide all the forms one by
        one...
        What's the project's startup object? From where do you open the WMP Form
        and the others? From where do you want to close them? Do you have an MDI
        Form? I do understand that you want to hide a Form (or some), but the
        relations between the Forms is still not clear to me. Which of the Forms
        "lives" always? Do you specify a Form as the Owner of other(s)? Are
        there modal Forms (related to the problem)?


        Armin

        Comment

        • kronecker@yahoo.co.uk

          #5
          Re: Closing all forms

          On May 3, 11:43 pm, "Armin Zingler" <az.nos...@free net.dewrote:
          <kronec...@yaho o.co.ukschrieb
          >
          >
          >
          I have an application which uses Windows media player on a separate
          form + other bits and pieces also on separate forms. I can close
          them all using the above code but for some reason when I close
          windows media player it will not open again unless I re-start the
          program. Therefore hiding it is a better thing to do so hiding
          rather than closing will do for me.
          However, one of my forms will not hide using the usual formx.hide( )
          method (it won't close either). So how to hide all the forms one by
          one...
          >
          What's the project's startup object? From where do you open the WMP Form
          and the others? From where do you want to close them? Do you have an MDI
          Form? I do understand that you want to hide a Form (or some), but the
          relations between the Forms is still not clear to me. Which of the Forms
          "lives" always? Do you specify a Form as the Owner of other(s)? Are
          there modal Forms (related to the problem)?
          >
          Armin
          Ok I figured it out. Clear all forms at once like this



          Private Const SW_HIDE As Integer = 0

          Private Const SW_RESTORE As Integer = 9

          Private Declare Function FindWindow Lib "user32.dll " Alias
          "FindWindow A" (ByVal lpClassName As String, ByVal lpWindowName As
          String) As Integer

          <DllImport("Use r32")_
          Private Shared Function ShowWindow(ByVa l hwnd As Integer, ByVal
          nCmdShow As Integer) As Integer

          Private Const SW_HIDE As Integer = 0

          Private Const SW_RESTORE As Integer = 9


          Public Sub hideallforms()
          ' Actually this hides them one by one
          Dim objType As Type() =
          Reflection.Asse mbly.GetExecuti ngAssembly.GetT ypes()
          Dim x As Integer, i As Integer

          Try
          For x = LBound(objType) To UBound(objType)
          If Not objType(x).Name = Me.Name Then '/// make sure
          you dont unload this form yet.
          i = FindWindow(vbNu llString, objType(x).Name )
          ShowWindow(i, SW_HIDE)

          End If
          Next
          Catch ex As Exception
          MessageBox.Show ("An error occured hiding forms:" &
          ex.Message)

          End Try
          End Sub
          Works a treat and you can restore them all at once too using
          SW_RESTORE. Now that wasn't hard was it..

          k.

          Comment

          • Armin Zingler

            #6
            Re: Closing all forms

            <kronecker@yaho o.co.ukschrieb
            Works a treat and you can restore them all at once too using
            SW_RESTORE. Now that wasn't hard was it..
            If you had answered some questions, I would have been able to give a
            solution, too. Anyway, problem seems to be "solved"...


            Armin

            Comment

            • kronecker@yahoo.co.uk

              #7
              Re: Closing all forms

              On May 4, 10:39 am, "Armin Zingler" <az.nos...@free net.dewrote:
              <kronec...@yaho o.co.ukschrieb
              >
              Works a treat and you can restore them all at once too using
              SW_RESTORE. Now that wasn't hard was it..
              >
              If you had answered some questions, I would have been able to give a
              solution, too. Anyway, problem seems to be "solved"...
              >
              Armin
              Thanks but I was after a general method rather than just specific to a
              particular case.

              K.

              Comment

              • Cor Ligthert[MVP]

                #8
                Re: Closing all forms

                Kronecker,

                Be aware that in Net almost everything exists from arrays, collections and
                lists (and generic versions of that)

                Every form has a childcollection , therefore this should do it if you have
                started your childforms from a form.

                \\\
                For i = Me.OwnedForms.C ount To -1 Step -1
                Me.OwnedForms(i ).Close()
                Next
                ///


                Backwards to be sure you peeling everytime one from the collection, confirm
                the so by me called "Armin Zingler" method.

                (Not tested by the way)

                Cor


                <kronecker@yaho o.co.ukschreef in bericht
                news:5da25abc-8325-4706-88a6-d6106c64549d@w4 g2000prd.google groups.com...
                >I found this nifty routine that closes a form one by one every time it
                is called. However, I need to hide them instead of closing them. Is
                there a way to alter the code? I assume it has something to do with
                this line here...
                >
                >
                PostMessage(i, CInt(&H10), vbNullString, vbNullString)
                >
                Thanks
                >
                k
                >
                >
                Public Sub closeallforms()
                Dim objType As Type() =
                Reflection.Asse mbly.GetExecuti ngAssembly.GetT ypes()
                Dim x As Integer, i As Integer
                Try
                For x = LBound(objType) To UBound(objType)
                If Not objType(x).Name = Me.Name Then '/// make sure
                you dont unload this form yet.
                i = FindWindow(vbNu llString, objType(x).Name )
                PostMessage(i, CInt(&H10), vbNullString,
                vbNullString)
                End If
                Next
                Catch ex As Exception
                MessageBox.Show ("Oops, the following error occured:" &
                ex.Message)
                Finally
                'MyBase.Close() '/// now that all the other forms are
                closed , unload this one.
                End Try
                End Sub
                >

                Comment

                • Armin Zingler

                  #9
                  Re: Closing all forms

                  <kronecker@yaho o.co.ukschrieb
                  On May 4, 10:39 am, "Armin Zingler" <az.nos...@free net.dewrote:
                  <kronec...@yaho o.co.ukschrieb
                  Works a treat and you can restore them all at once too using
                  SW_RESTORE. Now that wasn't hard was it..
                  If you had answered some questions, I would have been able to give
                  a solution, too. Anyway, problem seems to be "solved"...

                  Armin
                  >
                  Thanks but I was after a general method rather than just specific to
                  a particular case.
                  I think it's a very bad method in any case. But again, use it if it
                  works for you.

                  Maybe I should be fair and give some reasons:
                  - There are managed ways instead of using pInvoke.
                  - hWnd must be IntPtr. Yes, Integer works on a 32-bit System. Not on
                  64-bit systems. I'd always use Intptr without thinking about the
                  destination platform
                  - Well... the whole approach is b...sh.. Yes, even if it works. Sry, I
                  can't explain - but I bet i'd win every voting 95:5. ;-)



                  Armin

                  Comment

                  • Armin Zingler

                    #10
                    Re: Closing all forms

                    "Cor Ligthert[MVP]" <notmyfirstname @planet.nlschri eb
                    Backwards to be sure you peeling everytime one from the collection,
                    confirm the so by me called "Armin Zingler" method.
                    I'm still trying to understand whether this a positive or negative
                    statement. ;)


                    Armin

                    Comment

                    • Cor Ligthert[MVP]

                      #11
                      Re: Closing all forms

                      >
                      I'm still trying to understand whether this a positive or negative
                      statement. ;)
                      >
                      Why?

                      Cor

                      Comment

                      • Cor Ligthert[MVP]

                        #12
                        Re: Closing all forms

                        Armin,

                        I will make it easier for you, I don't know if you invented it, but in my
                        idea was you the first one from which I readed it.

                        Therefore I call it an Armin Zingler method.

                        I have to Herfried methods, which is the UrlEncoded and there is an terrible
                        good solution he had with the favorites.

                        When I have to do something with Serializing, then I always have to think on
                        Tom.

                        So decide your self if it is a positive or negative statement.

                        :-)

                        Cor

                        "Armin Zingler" <az.nospam@free net.deschreef in bericht
                        news:e97gnderIH A.3900@TK2MSFTN GP05.phx.gbl...
                        "Cor Ligthert[MVP]" <notmyfirstname @planet.nlschri eb
                        >Backwards to be sure you peeling everytime one from the collection,
                        >confirm the so by me called "Armin Zingler" method.
                        >
                        I'm still trying to understand whether this a positive or negative
                        statement. ;)
                        >
                        >
                        Armin

                        Comment

                        • kronecker@yahoo.co.uk

                          #13
                          Re: Closing all forms

                          On May 4, 9:16 pm, "Armin Zingler" <az.nos...@free net.dewrote:
                          <kronec...@yaho o.co.ukschrieb
                          >
                          On May 4, 10:39 am, "Armin Zingler" <az.nos...@free net.dewrote:
                          <kronec...@yaho o.co.ukschrieb
                          >
                          Works a treat and you can restore them all at once too using
                          SW_RESTORE. Now that wasn't hard was it..
                          >
                          If you had answered some questions, I would have been able to give
                          a solution, too. Anyway, problem seems to be "solved"...
                          >
                          Armin
                          >
                          Thanks but I was after a general method rather than just specific to
                          a particular case.
                          >
                          I think it's a very bad method in any case. But again, use it if it
                          works for you.
                          >
                          Maybe I should be fair and give some reasons:
                          - There are managed ways instead of using pInvoke.
                          - hWnd must be IntPtr. Yes, Integer works on a 32-bit System. Not on
                          64-bit systems. I'd always use Intptr without thinking about the
                          destination platform
                          - Well... the whole approach is b...sh.. Yes, even if it works. Sry, I
                          can't explain - but I bet i'd win every voting 95:5. ;-)
                          >
                          Armin
                          I kind of agree but it works for the present - yes it's a bodge and
                          not elegant.

                          K.

                          Comment

                          Working...