Dynamic Dialog : Edit Control Message Handling and DoDataExchange
Hi All,
I am facing a problem related to the DoDataExchange for the Controls on
the Dialog Box in a MFC dialog based application.
I will explain my architecture first.
I have a MFC dialog with two static buttons on it. I want to create the
dynamic controls on the client area of the dialog box.
For this I am using the my subclasses derived from the default
controls, lets say Edit Control.
After creating my class, MyEdit from the CEdit class, I am using
another class as a container to hold all the objects of the different
controls.
I am exposing this class to the application's dialog.
In the main applications OnInitDialog() , I create the Edit control
with the help of the container class.
It creates the class successfully. But now, I have some variables in
the container class which I want to associate with this Edit control
using the DoDataExchange.
I have defined my own DoDataExcahange in the container class...
The control resource I am getting dynamically. So when I give this ID
in the DDX_Text function, it throws the exception saying teh Exchange
controls is not associated with this variable.
//This is my class.
class DynamicEdit: public CEdit
{
DECLARE_DYNAMIC(DynamicEdit)
protected:
//Subclassed this message
afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags);
DECLARE_MESSAGE_MAP ()
public:
DynamicEdit();
CString m_strName;
//~DynamicEdit();
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
};
//Container class
class MyControls: public CWnd
{
public:
MyControls();
DynamicEdit *m_DynamicEdit;
BOOL Create(CWnd *pParent,CRect rect, int nID, int Type);
//~MyControls();
protected:
afx_msg void OnChar (UINT nChar, UINT nRepCnt, UINT nFlags);
DECLARE_MESSAGE_MAP()
//The data exchange for all the controls
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
};
Implementation file::
//Message Map for the Edit Control
BEGIN_MESSAGE_MAP (DynamicEdit, CEdit)
ON_WM_CHAR ()
END_MESSAGE_MAP ()
void DynamicEdit::DoDataExchange(CDataExchange* pDX)
{
CWnd::DoDataExchange(pDX);
DDX_Text(pDX,1021,m_strName); <===== Error
//DDV_MaxChars(pDX,m_strName,10);
}
Here the Edit control is displayed properly on the dialog but the Data
Exchange could not be done.
I am using the Main dialog's handle to create the controls on it. So I
want to delegate the message to my container class and use the
DoDataExchange of my custom control class.
How could this be done?
I am not getting any idea if we could attach a resource dynamically to
the variable as it always fails..
Also, If I am to create the Dynamic controls at run time , will this
approach work ( Container class)...
I desparately want to use the DDX and DDV routines instead of using the
GetDLGItem API's...
Is there any way to solve this.
Any help in this regard is highly appreciable.