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
topRequirements
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 topCreate 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.
- 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. - In the Generic C++ Class wizard, type
FormsCollection in the Class name box,
type CollectionBase in the Base class
box, and then click Finish.
- Open the FormsCollection.cpp file, and then delete the code
for the constructor and the code for the destructor.
- Open the FormsCollection.h file, and then replace the
existing code with the following code.
#pragma once
using namespace System::Collections;
using namespace System::Windows::Forms;
public __gc class FormsCollection : public CollectionBase
{
public:
Form* Add(Form *FormObject)
{
List->Add((FormObject));
return(FormObject) ;
}
public:
void Remove(Form *FormObject)
{
List->Remove(FormObject);
}
};
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:
- Add the following header file declaration in Form1.h.
#include "FormsCollection.h"
- Add a FormsCollection variable to the Form1.h file. To do this,
add the following code in the Form1 class.
public:
FormsCollection *Forms;
- In the Form1 constructor, after the InitializeComponent function call, add the following code to create an instance of
the Forms class.
Forms = new FormsCollection();
back to the topAdd 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 topUse 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:
- On the Project menu, click Add New
Item.
- 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. - In the Name text box, type
Form2, and then click Open.
- Open the Form2 form in Design view.
- Add a ComboBox control to
Form2.
- Open the Form1 form in Design view.
- Add a Button control and a
TreeView control to Form1.
- Add the following header file declaration in Form1.h:
#include "Form2.h"
- Add the following member variable to the Form1 class.
public:
Form2 *form2;
- 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);
- To remove the Form2 form from the collection, add the
following code in the Dispose of the Form1 class.
Forms->Remove(form2);
- 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);
}
}
back to the topBuild and test the application
- Press the CTRL+ SHIFT+B key combination to build the application.
- Press the CTRL+F5 key combination to run the application.
- Click button1. You notice that the message boxes display the control names
and the form name where the controls belong.
back to the
top