Call DDX_ManagedControl in
The following example shows how to bind a native C++ string to a .NET user control.
Example
The following is an example of DDX/DDV data binding of an MFC string m_str with the user-defined NameText property of a .NET user control.
The control is created when CMyDlg::DoDataExchange for the first time, so any code that references m_UserControl must come after the DDX_ManagedControl call.
You can implement this code in the MFC01 application you created in How to: Create the User Control and Host in a Dialog Box.
Put the following code in the declaration of CMFC01Dlg:
| В | Copy Code |
|---|---|
class CMFC01Dlg : public CDialog
{
CWinFormsControl<WindowsControlLibrary1::UserControl1> m_MyControl;
CString m_str;
}; | |
Put the following code in the implementation of CMFC01Dlg:
| В | Copy Code |
|---|---|
void CMFC01Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_ManagedControl(pDX, IDC_CTRL1, m_MyControl);
if (pDX->m_bSaveAndValidate) {
m_str = m_MyControl->textBox1->Text;
} else
{
m_MyControl->textBox1->Text = gcnew System::String(m_str);
}
} | |
Now we will add the handler method for a click on the OK button. Click the Resource View tab. In Resource View, double-click on IDD_MFC01_DIALOG. The dialog resource appears in Resource Editor. Then double click the OK button..
Define the handler as follows.
| В | Copy Code |
|---|---|
void CMFC01Dlg::OnBnClickedOk()
{
AfxMessageBox(CString(m_MyControl.GetControl()->textBox1->Text));
OnOK();
} | |
And add the following line to the implementation of BOOL CMFC01Dlg::OnInitDialog().
| В | Copy Code |
|---|---|
m_MyControl.GetControl()->textBox1->Text = "hello"; | |
You can now build and run the application. Notice that any text in the text box will be displayed in a pop-up message box when the application closes.
Dhtml editor
Free javascript download