Re: Dynamic CEdit access
el Scorcho wrote:
I have a dialog that has a number of CEdit controls on it and am
attempting to set the text in the control dynamically but am having a
hard getting it to work. I understand that I can get a pointer to the
control by using the GetDlgItem function and passing it the ID of the
control. My issue is that I don't want to have to hard-code each
controls ID and want to be able to somehow build it on the fly. For
example, say each of my CEdit controls has an ID like "IDC_EDIT1",
IDC_EDIT2, IDC_EDIT3..., and I want to populate each control's text
from data returned from the database on the fly as I roll-thru the
result set. Is there a way to build my ID to use in the GetDlgItem()
method on the fly?
The simple answer is that GetDlgItem can accept a variable (such as a
loop iterator) just as well as it can accept a hard-coded constant.
But a much cleaner solution is available, called a control array:
CEdit m_edits[MAX_EDITS];
You can initialize these things, eliminating all use of GetDlgItem.
// in DoDataExchange
for (int r = 0; r < MAX_EDITS; r++)
DDX_Control(pDX, IDC_EDIT1 + r, m_edits[r]);
Updating one of these controls is then as simple as...
m_edits[n].SetWindowText("blah blah blah");
--
Scott McPhillips [VC++ MVP]