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 to close all form but not application.exit()

How to close all form but not application.exit()

Scheduled Pinned Locked Moved C#
questiontutorial
13 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.
  • N nuttynibbles

    hi, i have a main form. it has list view of items that when clicked will open up another form without closing the main form. i did a inactivity timeout where user will be require to login again. if that happen, how do i close all current open form??

    OriginalGriffO Offline
    OriginalGriffO Offline
    OriginalGriff
    wrote on last edited by
    #3

    Application.OpenForms will return a Forms[] for all open forms. You can for loop through it closing the ones you no longer need (with Form.Close(), obviously). Do not try to foreach loop through, as the Close() may remove it from the array.

    If Barbie is so popular, why do you have to buy her friends? Eagles may soar, but weasels don't get sucked into jet engines. If at first you don't succeed, destroy all evidence that you tried.

    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
    "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

    N 1 Reply Last reply
    0
    • N N a v a n e e t h

      You need to keep form objects that are open in a collection. When relogin requires, loop through this list and dispose everything.

      Best wishes, Navaneeth

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #4

      N a v a n e e t h wrote:

      loop through this list and dispose closeeverything.

      Don't use dispose on a form that is currently displayed - unpredictable things may occur!

      If Barbie is so popular, why do you have to buy her friends? Eagles may soar, but weasels don't get sucked into jet engines. If at first you don't succeed, destroy all evidence that you tried.

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      N 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        Application.OpenForms will return a Forms[] for all open forms. You can for loop through it closing the ones you no longer need (with Form.Close(), obviously). Do not try to foreach loop through, as the Close() may remove it from the array.

        If Barbie is so popular, why do you have to buy her friends? Eagles may soar, but weasels don't get sucked into jet engines. If at first you don't succeed, destroy all evidence that you tried.

        N Offline
        N Offline
        nuttynibbles
        wrote on last edited by
        #5

        im implementing windows mobile using compact framework so there isn't application.openforms. is there something else similar?

        OriginalGriffO 1 Reply Last reply
        0
        • OriginalGriffO OriginalGriff

          N a v a n e e t h wrote:

          loop through this list and dispose closeeverything.

          Don't use dispose on a form that is currently displayed - unpredictable things may occur!

          If Barbie is so popular, why do you have to buy her friends? Eagles may soar, but weasels don't get sucked into jet engines. If at first you don't succeed, destroy all evidence that you tried.

          N Offline
          N Offline
          N a v a n e e t h
          wrote on last edited by
          #6

          When I say dispose everything, I didn't meant to call Dispose() method. BTW, how it can cause unpredictable things? :confused:

          Best wishes, Navaneeth

          OriginalGriffO 1 Reply Last reply
          0
          • N nuttynibbles

            im implementing windows mobile using compact framework so there isn't application.openforms. is there something else similar?

            OriginalGriffO Offline
            OriginalGriffO Offline
            OriginalGriff
            wrote on last edited by
            #7

            Not unless you create it yourself - keep a copy of the form object when you do the create, chain into the FormClosing event and remove it when it closes. You can then manually Close() each open instance.

            If Barbie is so popular, why do you have to buy her friends? Eagles may soar, but weasels don't get sucked into jet engines. If at first you don't succeed, destroy all evidence that you tried.

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

            N 1 Reply Last reply
            0
            • N N a v a n e e t h

              When I say dispose everything, I didn't meant to call Dispose() method. BTW, how it can cause unpredictable things? :confused:

              Best wishes, Navaneeth

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #8

              If the form has a background thread working, it could be using resources that you have disposed.

              If Barbie is so popular, why do you have to buy her friends? Eagles may soar, but weasels don't get sucked into jet engines. If at first you don't succeed, destroy all evidence that you tried.

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              1 Reply Last reply
              0
              • OriginalGriffO OriginalGriff

                Not unless you create it yourself - keep a copy of the form object when you do the create, chain into the FormClosing event and remove it when it closes. You can then manually Close() each open instance.

                If Barbie is so popular, why do you have to buy her friends? Eagles may soar, but weasels don't get sucked into jet engines. If at first you don't succeed, destroy all evidence that you tried.

                N Offline
                N Offline
                nuttynibbles
                wrote on last edited by
                #9

                hi OriginalGriff, what you mean is to create a class which act as a application hub that store all created form instances?? im sorry but wat do u mean by chain into the formClosing event?

                OriginalGriffO 1 Reply Last reply
                0
                • N nuttynibbles

                  hi OriginalGriff, what you mean is to create a class which act as a application hub that store all created form instances?? im sorry but wat do u mean by chain into the formClosing event?

                  OriginalGriffO Offline
                  OriginalGriffO Offline
                  OriginalGriff
                  wrote on last edited by
                  #10

                  Store them as part of your main form, as either an array of Form objects, or as individual YourForm references. At the moment, you do something like

                  YourForm yf = new YourForm();
                  yf.Show();

                  when the use clicks one of your list view entries - just move the "YourForm yf" outside the method (and give it a better name). You then change it to

                  yf = new YourForm();
                  yf.FormClosing += new EventHandler(YourFormClosing);
                  yf.Show();

                  and add

                  private void YourFormClosing(object sender, EventArgs e)
                  {
                  yf = null;
                  }

                  to remove it when it closes. You can then check

                  if (yf != null)
                  {
                  yf.Close();
                  }

                  If Barbie is so popular, why do you have to buy her friends? Eagles may soar, but weasels don't get sucked into jet engines. If at first you don't succeed, destroy all evidence that you tried.

                  "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                  "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                  N 1 Reply Last reply
                  0
                  • N nuttynibbles

                    hi, i have a main form. it has list view of items that when clicked will open up another form without closing the main form. i did a inactivity timeout where user will be require to login again. if that happen, how do i close all current open form??

                    realJSOPR Offline
                    realJSOPR Offline
                    realJSOP
                    wrote on last edited by
                    #11

                    You could simply call Form.Hide() instead of closing the forms outright.

                    .45 ACP - because shooting twice is just silly
                    -----
                    "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                    -----
                    "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

                    1 Reply Last reply
                    0
                    • OriginalGriffO OriginalGriff

                      Store them as part of your main form, as either an array of Form objects, or as individual YourForm references. At the moment, you do something like

                      YourForm yf = new YourForm();
                      yf.Show();

                      when the use clicks one of your list view entries - just move the "YourForm yf" outside the method (and give it a better name). You then change it to

                      yf = new YourForm();
                      yf.FormClosing += new EventHandler(YourFormClosing);
                      yf.Show();

                      and add

                      private void YourFormClosing(object sender, EventArgs e)
                      {
                      yf = null;
                      }

                      to remove it when it closes. You can then check

                      if (yf != null)
                      {
                      yf.Close();
                      }

                      If Barbie is so popular, why do you have to buy her friends? Eagles may soar, but weasels don't get sucked into jet engines. If at first you don't succeed, destroy all evidence that you tried.

                      N Offline
                      N Offline
                      nuttynibbles
                      wrote on last edited by
                      #12

                      hmm okay i will go try it out. tks again Original Griff

                      1 Reply Last reply
                      0
                      • N nuttynibbles

                        hi, i have a main form. it has list view of items that when clicked will open up another form without closing the main form. i did a inactivity timeout where user will be require to login again. if that happen, how do i close all current open form??

                        S Offline
                        S Offline
                        SilimSayo
                        wrote on last edited by
                        #13

                        If the forms are mdi forms,look at this http://www.codeproject.com/Messages/849885/Re-Closing-all-mdichildren-forms.aspx[^]

                        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