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. close all opened windows

close all opened windows

Scheduled Pinned Locked Moved C#
question
6 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.
  • M Offline
    M Offline
    MathewPV
    wrote on last edited by
    #1

    Hello all, Could somebody tell me how can i close all the windows opened in my windows application. The flow is like this from login to main window from main window to all other windows. when i click the logout button in main window it should close all the opened windows in my application and the login window must be shown.. Thanks in Advance..

    OriginalGriffO L M 3 Replies Last reply
    0
    • M MathewPV

      Hello all, Could somebody tell me how can i close all the windows opened in my windows application. The flow is like this from login to main window from main window to all other windows. when i click the logout button in main window it should close all the opened windows in my application and the login window must be shown.. Thanks in Advance..

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

      The easiest way is to make you login form your default form. When the user logs in correctly, show your "real" main form, and Hide the Login form. Then all you have to do is chain into the various close events: Login Form (on login ok):

              MainForm mf = new MainForm();
              mf.Show();
              mf.FormClosing += new FormClosingEventHandler(mf\_FormClosing);
              Hide();
      

      Login Form (event handler):

          void mf\_FormClosing(object sender, FormClosingEventArgs e)
              {
              Show();
              }
      

      At this point, you main form will show when you log in, and the login form will reappear when the main form closes - so your logout button just needs to call "Close()". Then when you construct each child form, give it a single parameter constructor:

          public ChildForm(Form parent)
              {
              InitializeComponent();
              if (parent != null)
                  {
                  parent.FormClosing += new FormClosingEventHandler(ParentForm\_FormClosing);
                  }
              }
      
          void ParentForm\_FormClosing(object sender, FormClosingEventArgs e)
              {
              Close();
              }
      

      Then, when the forms parent close, so will the child. So, construct your child forms as necessary:

              ChildForm cf = new ChildForm(this);
              cf.Show();
      

      And when the main form closes, the child forms will also close, and the log in screen be re-displayed.

      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
      • M MathewPV

        Hello all, Could somebody tell me how can i close all the windows opened in my windows application. The flow is like this from login to main window from main window to all other windows. when i click the logout button in main window it should close all the opened windows in my application and the login window must be shown.. Thanks in Advance..

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        Are you aware of Application.OpenForms? :)

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


        I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
        [The QA section does it automatically now, I hope we soon get it on regular forums as well]


        M 1 Reply Last reply
        0
        • L Luc Pattyn

          Are you aware of Application.OpenForms? :)

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


          I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
          [The QA section does it automatically now, I hope we soon get it on regular forums as well]


          M Offline
          M Offline
          MathewPV
          wrote on last edited by
          #4

          Sorry i am not aware of application.openforms. Can i get the name of the open windows using this property.

          L 1 Reply Last reply
          0
          • M MathewPV

            Sorry i am not aware of application.openforms. Can i get the name of the open windows using this property.

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            You get an array of Form references, as the documentation would tell you. :|

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


            I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
            [The QA section does it automatically now, I hope we soon get it on regular forums as well]


            1 Reply Last reply
            0
            • M MathewPV

              Hello all, Could somebody tell me how can i close all the windows opened in my windows application. The flow is like this from login to main window from main window to all other windows. when i click the logout button in main window it should close all the opened windows in my application and the login window must be shown.. Thanks in Advance..

              M Offline
              M Offline
              Member 4349793
              wrote on last edited by
              #6

              Mohan, try this. Call this method in the Button Click event. public static void CloseAllForms() { //Create a Collection to Store all Opened Forms. List<Form> formsList = new List<Form>(); //All all opened forms into a Collection. foreach (Form frm in Application.OpenForms) { //Execulde the Current Form. if (frm.Name == "Form1") continue; else formsList.Add(frm); } //Now Close the forms foreach (Form frm in formsList) { frm.Close(); } }

              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