Set 'visible' property to 'false' for all the opened forms
-
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.
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
-
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.
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 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; } } }
MartinThanks 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.
-
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
-
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.
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 -
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 anArrayList
or a custom, typoesafe class derived fromCollectionBase
.
"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
-
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 -
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?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