You are here:

C++/An array of edit boxes

Advertisement


Question
QUESTION: Hi Bill,

My program has multiple edit boxes and I would like to make an array of the edit boxes. Since MSVC++ 6 doesn't allow arrays of edit boxes directly, can I make an array of pointers to the edit boxes:

I've tried:
CEdit* pEdit[0] = (CEdit*)GetDlgItem(IDC_EDIT_FIRST)?

Also tried:
CEdit* array = new CEdit[numEditsInApp];
array[0] = GetDlgItem(ID);

I can't seem to make either of these work, but can you suggest something? How should I initialize an array of pointers?

Thanks in advance for your help,
-Neil


ANSWER: You can create CEdit arrays.  You have to make the connection to the dialog items in the DoDataExchange function:

If you have 10 CEdits:

CEdit m_cedit[10];

then you have 10 lines in your DoDataExchange:

DDX_Control(pDX, IDC_EDIT1, m_cedit[0]);
DDX_Control(pDX, IDC_EDIT2, m_cedit[0]);
.
.
.
DDX_Control(pDX, IDC_EDIT10, m_cedit[9]);

This connects the variables to the GUI edit boxes.

You can use

for( int idx = 0; idx < 10; ++idx )
 m_cedit[idx].SetWindowText( "Hello World" );

to set all of the edit boxes.

Hope this helps you.

Bill

---------- FOLLOW-UP ----------

QUESTION: Hi again, Bill.

Though I'm fine with most of this, I'm lost on how to declare the pointer pDX. MSDN says that it's "a pointer to a CDataExchange object" but doesn't explain how to do this. Should it be: pDX->IDC_EDIT1 or CEdit* pDX? Neither seem to work and I'm lost...

Thanks in advance for your help,
-Neil

ANSWER: Hi Neil,

Ok, DoDataExchange is used in Dialogs.  Where I have multiple items like this (I have dialogs with 30+ CButton checkboxes) they're in dialogs.

The pDX is passed to you in the DoDataExchange.  If you are in a Dialog, the framework should have created a DoDataExchange.  Is this not a dialog?  If not, then GetDlgItem shouldn't have been valid in your first attempt.

Bill

---------- FOLLOW-UP ----------

QUESTION: Hi again, Bill.

You've lost me here. When I set up your example, all passed muster excepting 'pDX' which MSC++6 said was in error because it was an "undeclared identifier". Checking through MSDN, I found many references to 'pDX', but never a reference as to how I should declare it. Your example is used in a dialog, so that is OK. I guess that 'DDX_Control' is the DoDataExchange, but that still doesn't seem to satisfy the undeclared identifier.

-Neil

Answer
Hi Neil,

Did you have a DoDataExchange in your example?  All of my dialogs, when created, get this member function in the class for the dialog.

Ok, second question: Do you have a class associated with the dialog?  If you right-click a control in the dialog and "Add variable" is grayed out, then you don't have one.  Once you set one up (when you created the dialog you're asked for a class to use, which is filled in by default and I always accept).

The problem isn't pDX, because those lines are in the DoDataExchange function where pDX is a parameter to the function.  Maybe DoDataExchange is created only after one variable is assigned (I typically assign a variable to all controls except labels and buttons I won't change the state of).

We'll solve this I'm sure.  Might take a few more cycles.

Bill

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Bill A

Expertise

I can answer questions about C++, programming algorithms, Windows programming in MFC (which is C++). I cannot answer questions about STL (templates) and I have no experience with Linux. I do enjoy reviewing code and critiquing it or finding problems in it. I will also gladly show better algorithms or methods if you want to take advantage of that.

Experience

I've developed a commercial embedded C compiler/assembler and IDE with debugger toolset, of which the IDE and debugger are written in C++. I work in the industry writing high tech embedded programs and Windows programs to communicate with the embedded devices.

Publications
Book: Embedded Systems Design using the Rabbit 3000 Microprocessor Authored Chapter 10 in its entirety.

Education/Credentials
BS Computer Engineering

©2016 About.com. All rights reserved.