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. Set 'visible' property to 'false' for all the opened forms

Set 'visible' property to 'false' for all the opened forms

Scheduled Pinned Locked Moved C#
question
9 Posts 3 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.
  • A Offline
    A Offline
    AngryC
    wrote on last edited by
    #1

    Hello, I want to pass over all the opened forms of my application and set the visible property to false, if the form was shown. How can I do that? Thanks.

    S M 2 Replies Last reply
    0
    • A AngryC

      Hello, I want to pass over all the opened forms of my application and set the visible property to false, if the form was shown. How can I do that? Thanks.

      S Offline
      S Offline
      Stefan Troschuetz
      wrote on last edited by
      #2

      Whenever you open a form, store the reference to it in some central collection (don't forget to remove after closing the form). Now, if you want to hide all forms, iterate over the collection and assign false to the Visible property.


      "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

      www.troschuetz.de

      A 1 Reply Last reply
      0
      • A AngryC

        Hello, I want to pass over all the opened forms of my application and set the visible property to false, if the form was shown. How can I do that? Thanks.

        M Offline
        M Offline
        MGoettmann
        wrote on last edited by
        #3

        a quick and dirty way::) using System; using System.Runtime.InteropServices; using System.Windows.Forms; public class GetFormsCollection { private delegate bool WndEnumProc(System.IntPtr hWnd , int lParam); [DllImport("user32.dll", CharSet=CharSet.Auto)] private static extern bool EnumWindows (WndEnumProc lpEnumFunc, int lParam); public readonly System.Collections.ArrayList Forms; public GetFormsCollection() { Forms = new System.Collections.ArrayList(); EnumWindows(new WndEnumProc(EnumCallback), 0); } private bool EnumCallback(System.IntPtr hWnd, int lParam) { Control hForm = System.Windows.Forms.Control.FromHandle(hWnd); // hForm is null for handles not created by your app... if (hForm != null) this.Forms.Add(hForm); return true; } public static void SetVisiblity(bool fVisible) { foreach (System.Windows.Forms.Form objForm in new GetFormsCollection().Forms) { objForm.Visible = fVisible; } } } Martin

        A 1 Reply Last reply
        0
        • M MGoettmann

          a quick and dirty way::) using System; using System.Runtime.InteropServices; using System.Windows.Forms; public class GetFormsCollection { private delegate bool WndEnumProc(System.IntPtr hWnd , int lParam); [DllImport("user32.dll", CharSet=CharSet.Auto)] private static extern bool EnumWindows (WndEnumProc lpEnumFunc, int lParam); public readonly System.Collections.ArrayList Forms; public GetFormsCollection() { Forms = new System.Collections.ArrayList(); EnumWindows(new WndEnumProc(EnumCallback), 0); } private bool EnumCallback(System.IntPtr hWnd, int lParam) { Control hForm = System.Windows.Forms.Control.FromHandle(hWnd); // hForm is null for handles not created by your app... if (hForm != null) this.Forms.Add(hForm); return true; } public static void SetVisiblity(bool fVisible) { foreach (System.Windows.Forms.Form objForm in new GetFormsCollection().Forms) { objForm.Visible = fVisible; } } } Martin

          A Offline
          A Offline
          AngryC
          wrote on last edited by
          #4

          Thanks a lot, but I got an error: Unable to cast object of type 'System.Windows.Forms.ToolStripDropDownMenu' to type 'System.Windows.Forms.Form'. On this line: foreach (System.Windows.Forms.Form objForm in new GetFormsCollection().Forms) I'm using .NET 2.0 What wrong? Thanks.

          M 1 Reply Last reply
          0
          • S Stefan Troschuetz

            Whenever you open a form, store the reference to it in some central collection (don't forget to remove after closing the form). Now, if you want to hide all forms, iterate over the collection and assign false to the Visible property.


            "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

            www.troschuetz.de

            A Offline
            A Offline
            AngryC
            wrote on last edited by
            #5

            Can you provide me with some code, please?

            S 1 Reply Last reply
            0
            • A AngryC

              Thanks a lot, but I got an error: Unable to cast object of type 'System.Windows.Forms.ToolStripDropDownMenu' to type 'System.Windows.Forms.Form'. On this line: foreach (System.Windows.Forms.Form objForm in new GetFormsCollection().Forms) I'm using .NET 2.0 What wrong? Thanks.

              M Offline
              M Offline
              MGoettmann
              wrote on last edited by
              #6

              oops, I'm using VS 2003 so I can't check that... As I said, it's a quick & dirty way;). Creating a Forms.Control object from a handle seems to work for some other UI controls (i.e. ToolStripDropDownMenu) that respond to EnumWindows(). However, try to replace the line Control hForm = System.Windows.Forms.Control.FromHandle(hWnd); with this one: System.Windows.Forms.Form hForm = System.Windows.Forms.Control.FromHandle(hWnd) as System.Windows.Forms.Form; in order to get forms only. hope that helps Martin

              A 1 Reply Last reply
              0
              • A AngryC

                Can you provide me with some code, please?

                S Offline
                S Offline
                Stefan Troschuetz
                wrote on last edited by
                #7

                It's almost impossible without knowing your code. Declare the central form collection where it csn be accessed whenever a fom is opened (either in the main form that opens all other forms or as a static property or class which is always accesssible). If you're using .NEt 2.0 use a generic List as collection otherwise an ArrayList or a custom, typoesafe class derived from CollectionBase.


                "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

                www.troschuetz.de

                1 Reply Last reply
                0
                • M MGoettmann

                  oops, I'm using VS 2003 so I can't check that... As I said, it's a quick & dirty way;). Creating a Forms.Control object from a handle seems to work for some other UI controls (i.e. ToolStripDropDownMenu) that respond to EnumWindows(). However, try to replace the line Control hForm = System.Windows.Forms.Control.FromHandle(hWnd); with this one: System.Windows.Forms.Form hForm = System.Windows.Forms.Control.FromHandle(hWnd) as System.Windows.Forms.Form; in order to get forms only. hope that helps Martin

                  A Offline
                  A Offline
                  AngryC
                  wrote on last edited by
                  #8

                  Thanks a lot, it worked! :) But... It doesn't hide the print preview dialog of my internal WebBrowser control! WebBrowser1.ShowPageSetupDialog(); Is there any way to hide that as well?

                  M 1 Reply Last reply
                  0
                  • A AngryC

                    Thanks a lot, it worked! :) But... It doesn't hide the print preview dialog of my internal WebBrowser control! WebBrowser1.ShowPageSetupDialog(); Is there any way to hide that as well?

                    M Offline
                    M Offline
                    MGoettmann
                    wrote on last edited by
                    #9

                    ouh... this becomes really complicated. To solve this particular issue, I would suggest to add an eventhandler in your form. this.VisibleChanged += new EventHandler(Form1_VisibleChanged); Here you can show/hide the dialog according to the actual form's Visible state. My solution is a sledgehammer method to get all running form objects - without having any idea of your application logic. Maybe you will run run into similar issues with other forms/controls. At this point you might consider implementing Stefan's solution and storing your forms references in a central list? btw: if *all* app's windows (incl. dialogs) were hidden... how to bring them back? regards Martin

                    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