Microsoft KB Archive/815706

= How to create a Forms collection in Visual C++ =

Article ID: 815706

Article Last Modified on 11/14/2007

-

APPLIES TO


 * Microsoft Visual C++ 2005 Express Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition

-







For a Microsoft Visual Basic .NET version of this article, see 308537.



For a Microsoft Visual C# .NET version of this article, see 815707.

This article refers to the following Microsoft .NET Framework Class Library namespaces:
 * System.Collections
 * System.Windows.Forms

IN THIS TASK

 * SUMMARY
 * Requirements
 * Create the custom Forms collection
 * Create an instance of the Forms collection object
 * Add and remove Windows forms to and from the collection
 * Use the Forms collection
 * Build and test the application
 * REFERENCES



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.

back to the top

Requirements
This article assumes that you are familiar with the following topics:
 * Visual C++ programming concepts

The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
 * Microsoft Visual C++ .NET 2003 or Microsoft Visual C++ 2005

back to the top

Create the custom Forms collection
The first step to create a custom collection class in Visual C++ .NET or in 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. To do this, follow these steps:  Start Microsoft Visual Studio .NET 2003 or Microsoft Visual Studio 2005. On the File menu, point to New, and then click Project. Click Visual C++ Projects under Project Types, and then click Windows Forms Application (.NET) under Templates.

Note In Visual C++ 2005, Visual C++ Projects is changed to Visual C++. In the Name text box, type Q815706 . In the Location text box, type C:\Test, and then click OK. On the Project menu, click Add Class.</li> In the Add Class dialog box, click Generic C++ Class under Templates, and then click Open.

Note In Visual C++ 2005, Generic C++ Class is changed to C++ Class.</li> In the Generic C++ Class wizard, type FormsCollection in the Class name box, type CollectionBase in the Base class box, and then click Finish.</li> Open the FormsCollection.cpp file, and then delete the code for the constructor and the code for the destructor.</li>  Open the FormsCollection.h file, and then replace the existing code with the following code. using namespace System::Collections; using namespace System::Windows::Forms;
 * 1) pragma once

public __gc class FormsCollection : public CollectionBase { public: Form* Add(Form *FormObject) {       List->Add((FormObject)); return(FormObject) ; }

public: void Remove(Form *FormObject) {       List->Remove(FormObject); } }; </li></ol>

back to the top

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 the Main method in the Project Properties dialog box.

To create an instance of a Forms collection object, follow these steps: <ol>  Add the following header file declaration in Form1.h.  Add a FormsCollection variable to the Form1.h file. To do this, add the following code in the Form1 class. public: FormsCollection *Forms; </li>  In the Form1 constructor, after the InitializeComponent function call, add the following code to create an instance of the Forms class. Forms = new FormsCollection; </li></ol>
 * 1) include &quot;FormsCollection.h&quot; </li>

back to the top

Add and remove Windows forms to and from the collection
Before you add a Windows form to the collection, you must add a Windows form to the project from the Project menu. You must modify the Dispose method so that the Windows form can remove itself from the collection.

Add the following code to the Form1 constructor to add the form to the collection. Forms->Add(this); To remove the Form1 form from the collection, append the following code in the Dispose method of the Form1 class. Forms->Remove(this); back to the top

Use the Forms collection
The following example loops through the Forms collection, and displays the Name and the Text property of any control on the form: <ol> On the Project menu, click Add New Item.</li> In the Add New Item dialog box, click Windows Form (.NET) under Templates.

Note In Visual C++ 2005, Windows Form (.NET) is changed to Windows Form.</li> In the Name text box, type Form2, and then click Open.</li> Open the Form2 form in Design view.</li> Add a ComboBox control to Form2.</li> Open the Form1 form in Design view.</li> Add a Button control and a TreeView control to Form1.</li>  Add the following header file declaration in Form1.h: <li> Add the following member variable to the Form1 class. public: Form2 *form2; </li> <li> Create an instance of the Form2 class, and then add the instance to the collection class. To do this, add following code to the constructor of the Form1 class. form2 = new Form2; Forms->Add(form2); </li> <li> To remove the Form2 form from the collection, add the following code in the Dispose of the Form1 class. Forms->Remove(form2); </li> <li> Double-click Button1 to open the code window, and then add the following code in the button1_Click event. IEnumerator* ie = __try_cast<IEnumerator*>(Forms->GetEnumerator); while (ie->MoveNext) {      Form *LoopForm = __try_cast<Form*>(ie->Current); IEnumerator* ie1 = __try_cast<IEnumerator*>(LoopForm->Controls->GetEnumerator); while(ie1->MoveNext) {       Control *LoopControl = __try_cast<Control*>(ie1->Current); MessageBox::Show(LoopControl->Name,LoopForm->Text); } } </li></ol>
 * 1) include &quot;Form2.h&quot; </li>

back to the top

Build and test the application

 * 1) Press the CTRL+ SHIFT+B key combination to build the application.
 * 2) Press the CTRL+F5 key combination to run the application.
 * 3) Click button1. You notice that the message boxes display the control names and the form name where the controls belong.

back to the top

<div class="references_section">