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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Closing all mdichildren forms

Closing all mdichildren forms

Scheduled Pinned Locked Moved C#
help
6 Posts 3 Posters 2 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.
  • D Offline
    D Offline
    Dylan van Heerden
    wrote on last edited by
    #1

    The following doesn't seem to work... foreach (Form f in this.MdiChildren) { f.Close(); } I recieve the following error "Specified cast is not valid." Am I missing something. :confused: Sweet

    H L 2 Replies Last reply
    0
    • D Dylan van Heerden

      The following doesn't seem to work... foreach (Form f in this.MdiChildren) { f.Close(); } I recieve the following error "Specified cast is not valid." Am I missing something. :confused: Sweet

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      You can't change a collection while enumerating it. Calling Close ultimately will change the collection. Instead, use:

      for (int i=0; i<MdiChildren; i++)
      {
      Form f = (Form)MdiChildren[i];
      f.Close();
      }

      Microsoft MVP, Visual C# My Articles

      D 1 Reply Last reply
      0
      • H Heath Stewart

        You can't change a collection while enumerating it. Calling Close ultimately will change the collection. Instead, use:

        for (int i=0; i<MdiChildren; i++)
        {
        Form f = (Form)MdiChildren[i];
        f.Close();
        }

        Microsoft MVP, Visual C# My Articles

        D Offline
        D Offline
        Dylan van Heerden
        wrote on last edited by
        #3

        Right now comes the this. I've got 3 mdichildren forms and when I click the button to close all the forms, only 2 closes and 1 stays open until I click the button again. Is it because I did the following like u showed except I added .Length. for (int i = 0; i < MdiChildren.Length; i++) { Form f = (Form)MdiChildren[i]; f.Close(); } Sweet

        H 1 Reply Last reply
        0
        • D Dylan van Heerden

          Right now comes the this. I've got 3 mdichildren forms and when I click the button to close all the forms, only 2 closes and 1 stays open until I click the button again. Is it because I did the following like u showed except I added .Length. for (int i = 0; i < MdiChildren.Length; i++) { Form f = (Form)MdiChildren[i]; f.Close(); } Sweet

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          Sorry - I made a mistake. You should either run through the array backward or make a copy of the array (and forgetting .Length was a typo - it's only sample code):

          for (int i=MdiChildren.Length; i >= 0; i--)
          {
          Form f = MdiChildren[i]; // Cast really not necessary
          f.Close();
          }

          Microsoft MVP, Visual C# My Articles

          D 1 Reply Last reply
          0
          • H Heath Stewart

            Sorry - I made a mistake. You should either run through the array backward or make a copy of the array (and forgetting .Length was a typo - it's only sample code):

            for (int i=MdiChildren.Length; i >= 0; i--)
            {
            Form f = MdiChildren[i]; // Cast really not necessary
            f.Close();
            }

            Microsoft MVP, Visual C# My Articles

            D Offline
            D Offline
            Dylan van Heerden
            wrote on last edited by
            #5

            Thanks it works. All I had to do is add the - 1 for (int i = MdiChildren.Length - 1; i >= 0; i--) { Form f = MdiChildren[i]; f.Close(); } The Author of the book I'm reading should revise his book. ;) My brain is 2 small for all the knowledge that I need.

            1 Reply Last reply
            0
            • D Dylan van Heerden

              The following doesn't seem to work... foreach (Form f in this.MdiChildren) { f.Close(); } I recieve the following error "Specified cast is not valid." Am I missing something. :confused: Sweet

              L Offline
              L Offline
              LongRange Shooter
              wrote on last edited by
              #6

              You were real close, here is my CloseAllChildren process for my MDIParent:

              	private void CloseAllChildren()
              	{
              		System.Windows.Forms.Form\[\] myChildren = this.MdiChildren;
              		foreach (Form child in myChildren)
              		{
              			child.Close();
              		}
              		return;
              	}
              

              ______________________________ The Tao gave birth to machine language. Machine language gave birth to the assembler. The assembler gave birth to ten thousand languages. Each language has its purpose, however humble. Each language expresses the Yin and Yang of software. Each language has its place within the Tao. Beauty exists because we give a name to C#. Bad exists because we give a name to COBOL.

              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