Need to create a control array in C++ 6 at runtime with an unknown number of elements

Posted on 2005-04-08
Last Modified: 2013-11-20
We have an MFC C++ 6 application which needs to show between 1 and 100 Edit controls. We won't know how many we need until the program is running, and it can change dynamcially as the user is using the program.

This would be easily solvable in VB6 using a Control Array, so we need similar capability, but in C++ 6.

We do not want to create all 100 controls in advance and hide them, we want the form at design time to have 1 (or none) of the Edit control. We want to do everything at runtime. We also need to destry (or at least hide) the controls during the process.

When we determine that we need an additional Edit control, we need to create it, position it, and set it visible at that time, then of course be able to access the value entered in it.

We are new to C++ 6, so please assume no prior code exists, and give us everything we need to implement this, basically from an MFC exe app created by the wizard as a starting point.

Thanks in advance!
Question by:mattxlr8
    LVL 30

    Expert Comment

    Hi mattxlr8,
    Exactly what type of controls are you trying to display?
    Are they all of the same type, or different types?

    David Maisonave :-)

    Author Comment

    They are CEdit controls in this example, but we would like to be able ultimately to do it to any control type. The number needed could start at 20, go to 75, and back to 5 during the same session.

    Matt Brown
    LVL 55

    Expert Comment

    by:Jaime Olivares
    Just use a CPtrArray to store pointers to controls.
    Create every control by calling new operator and then use proper Create() function, store the pointer into the CPtrArray.

    Author Comment


    Thanks for your response, but I need an actual code sample. We tried some experimentation with the Create function and got nowhere. Please re-read the requirements, and if you can, give me a somplete example (starting with a Dialog MFC app created with the Wizard).

    Thanks again, hope to hear more from you.

    Matt Brown

    Author Comment

    I answered my own question, I am posting it here in case others end up here in a search.

    First, simply declare the following Member variable of the dialog box

    CEdit* m_myeditarray;

    Now, in one button event I create the text boxes as follows:

          myeditarray = new CEdit[4];
          myeditarray[0].Create(ES_MULTILINE | WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER, CRect(10, 10, 60, 20), this, 1);
          myeditarray[2].Create(ES_MULTILINE | WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER, CRect(10, 30, 60, 40), this, 1);
          myeditarray[3].Create(ES_MULTILINE | WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER, CRect(10, 50, 60, 50), this, 1);
          myeditarray[4].Create(ES_MULTILINE | WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER, CRect(10, 70, 60, 60), this, 1);
          myeditarray[5].Create(ES_MULTILINE | WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER, CRect(10, 90, 60, 100), this, 1);

    The Create method actually generates the text box onto the form, and they can be used at this point

    When I want to retrieve the values in another button, I simply do this:


          CString out;


    LVL 30

    Expert Comment

    I would not take that route.

    I recommend you instead use a ListBox Control to store your controls.

    Check out the following link:

    You can use the above method with different types of controls, and it would be an interface that would be more familiar to users.
    LVL 6

    Accepted Solution

    // declare global
    CDWordArray g_dwaEditControls;

    CEdit* CreateANewEdit(CWnd* pParentWnd, DWORD dwStyle, const CRect& rect)
        CEdit* pEdit = new CEdit();
        pEdit->Create(dwStyle, rect, pParentWnd, g_dwaEditControls.GetSize() + 1);

        return pEdit;

    // to get a single edit by number
    CEdit* pEdit = (CEdit*)g_dwaEditControls.GetAt(nID - 1);
    pEdit->MoveWindow(...); // etc

    // At on Destroy of the application, clear the array of edits
    CEdit* pEdit;
        pEdit = (CEdit*)g_dwaEditControls.GetAt(0);
        delete pEdit;

    // and etc...

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
    Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now