How to create a Forms collection in Visual C#.NET or Visual C# 2005

Article translations Article translations
Article ID: 815707 - View products that this article applies to.
Expand all | Collapse all

On This Page

SUMMARY

Microsoft Visual C# .NET or Microsoft Visual C# 2005 does not provide a built-in collection for Form objects that are used in a project. This article describes how to build a custom collection class that essentially supports the functionality of a Forms collection.

Create the Custom Forms Collection

The first step to create a custom collection class in Visual C# .NET or Visual C# 2005 is to add a class to the project. To prevent objects other than Form objects from being added to the collection, make sure that this class inherits the CollectionBase class and then shadows the Add method.

The following code illustrates how to add this class:
   public class FormsCollection : CollectionBase
   {
      public  Form Add(Form FormObject) 
      {
         base.List.Add(FormObject);
         return(FormObject) ;
      }

      public void  Remove(Form FormObject) 
      {
         base.List.Remove(FormObject);
      }
   }

Create an Instance of the Forms Collection Object

When you run the project, you must create an instance of the custom collection class to add Form objects to it. The easiest way to make sure that the collection is created before any forms are displayed is to set the startup object for the project to the class that contains Main in the Project Properties dialog box.

The following code illustrates how to create an instance of a Forms collection object:
 public FormsCollection Forms;
   static void Main() 
   {
      Forms = new FormsCollection(); 
      Application.Run(new Form1());
   }

Add and Remove Forms from the Collection

Before you add a Form to the collection , you must add a Form to the project from the Project menu. After this, you must modify the constructor so that the Form can add itself to the collection, and you must modify the destructor so that the Form can remove itself from the collection.

The following code illustrates how to do this:
		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			Forms.Add(this);
			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
         Forms.Remove(this);
		}

Use the Forms Collection

The following example loops through the Forms collection and displays the Name property of any control on the form:
      Form LoopForm ;
      Control LoopControl;
   
      foreach ( Form LoopForm in Forms)
      {
         foreach (Control LoopControl in LoopForm.Controls)
         {
            MessageBox.Show(LoopControl.Name);
         }
      }

REFERENCES

For more information about the Microsoft .NET Framework Form class, visit the following Microsoft Web site:
http://msdn2.microsoft.com/en-us/library/system.windows.forms.form(vs.71).aspx

Properties

Article ID: 815707 - Last Review: November 14, 2007 - Revision: 3.4
APPLIES TO
  • Microsoft Visual C# .NET 2002 Standard Edition
  • Microsoft Visual C# 2005 Express Edition
Keywords: 
kbwindowsforms kbcollections kbcollectionclass kbprogramming kbnamespace kbforms kbhowtomaster KB815707

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com