How to create a Forms collection in Visual C++

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

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:
  1. Start Microsoft Visual Studio .NET 2003 or Microsoft Visual Studio 2005.
  2. On the File menu, point to
    New, and then click Project.
  3. 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++.
  4. In the Name text box, type
    Q815706.
  5. In the Location text box, type
    C:\Test, and then click OK.
  6. On the Project menu, click Add Class.
  7. 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.
  8. In the Generic C++ Class wizard, type
    FormsCollection in the Class name box, type CollectionBase in the Base classbox, and then click Finish.
  9. Open the FormsCollection.cpp file, and then delete the code for the constructor and the code for the destructor.
  10. 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 __gcclass 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:
  1. Add the following header file declaration in Form1.h.
    #include "FormsCollection.h"
  2. Add a FormsCollection variable to the Form1.h file. To do this, add the following code in the Form1 class.
    public:
    FormsCollection *Forms;
  3. 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 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:
  1. On the Project menu, click Add New Item.
  2. 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.
  3. In the Name text box, type
    Form2, and then click Open.
  4. Open the Form2 form in Design view.
  5. Add a ComboBox control to
    Form2.
  6. Open the Form1 form in Design view.
  7. Add a Button control and a
    TreeView control to Form1.
  8. Add the following header file declaration in Form1.h:
    #include "Form2.h"
  9. Add the following member variable to the Form1 class.
            public:
    Form2 *form2;
  10. 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);
  11. To remove the Form2 form from the collection, add the following code in the Dispose of the Form1 class.
    Forms->Remove(form2);
  12. 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 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

References

For more information about the Microsoft .NET Framework Form class, visit the following Microsoft Developer (MSDN) Web site: back to the top
Egenskaper

Artikel-id: 815706 – senaste granskning 14 nov. 2007 – revision: 1

Feedback