Link to home
Start Free TrialLog in
Avatar of mattxlr8
mattxlr8

asked on

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

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!
Matt
Avatar of Axter
Axter
Flag of United States of America image

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

David Maisonave :-)
Cheers!
Avatar of mattxlr8
mattxlr8

ASKER

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.

Thanks!
Matt Brown
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.
Jaime:

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
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:

      UpdateData();

      CString out;
      myeditarray[0].GetWindowText(out);
      MessageBox(out);

      myeditarray[1].GetWindowText(out);
      MessageBox(out);


etc.
I would not take that route.

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

Check out the following link:
http://www.codeguru.com/Cpp/controls/listbox/article.php/c4761/

You can use the above method with different types of controls, and it would be an interface that would be more familiar to users.
ASKER CERTIFIED SOLUTION
Avatar of nabehs
nabehs

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial