[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 322
  • Last Modified:

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
0
mattxlr8
Asked:
mattxlr8
1 Solution
 
AxterCommented:
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!
0
 
mattxlr8Author Commented:
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
0
 
Jaime OlivaresCommented:
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.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
mattxlr8Author Commented:
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
0
 
mattxlr8Author Commented:
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.
0
 
AxterCommented:
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.
0
 
nabehsCommented:
// 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);
    g_dwaEditControls.Add((DWORD)pEdit);

    return pEdit;
}

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

// At on Destroy of the application, clear the array of edits
CEdit* pEdit;
while(g_dwaEditControls.GetSize())
{
    pEdit = (CEdit*)g_dwaEditControls.GetAt(0);
    pEdit->DestroyWindow();
    delete pEdit;
    g_dwaEditControls.RemoveAt(0);
}
g_dwaEditControls.RemoveAll();

// and etc...
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now