OK im using a MFC appwizard and created a dialog based program. Now i am trying to change the text in a EDIT control i have placed on the form. How do i do this?
Printable View
OK im using a MFC appwizard and created a dialog based program. Now i am trying to change the text in a EDIT control i have placed on the form. How do i do this?
I guess i should have searched harder i learned it
Here :
VB Code:
CClipboardT1Dlg::SetDlgItemText(IDC_EDIT1,"Jason");
Given that you use MFC you could also create a control variable and use it's SetWindowText method.
Or better, create a control value variable and use DDX.
What is DDX and how do i create a control variable?
With the wizard, or by looking at how the wizard does it.
DDX stands for "Dynamic Data eXchange" and it's MFC's mechanism to bind variables to controls. It's built around the virtual method CDialog::DoDataExchange. This method is called indirectly by the CDialog::UpdateData method. You must override DoDataExchange and insert calls to the various (global) DDX_* and DDV_* functions. The most important of those are DDX_String (or DDX_Text, I forgot), which binds a string variable to an edit control and DDX_Control, which binds a control to a variable of the appropriate CWnd-derived control class.
Other functions retrieve an int that is the index of the selected radio button in a group of radio buttons or limit the text length of an edit control to a maximum.
that sounds so confusing. and complicated i rather do it my way but thanks alot ill look into it
It's very simple. But have it your way. It's better to do something complicated that you're familiar with than something easy that you have to learn (that is, under some circumstances).
So assuming you have an edit box with the ID IDC_NAME (it's a good idea to give your controls IDs with a meaning) and you want to bind it to a member variable called m_strName.
You'd do these steps:
1) Add the variable to the class declaration:
2) Add an override of DoDataExchange (if there isn't already one):Code:class CClipboardT1Dlg : public CDialog
{
// ...
CString m_strName;
// ...
};
Now the variable is bound. What's complicated about that? ;)Code:class CClipboardT1Dlg : public CDialog
{
// ...
CString m_strName;
// ...
virtual void DoDataExchange(CDataExchange *pDX);
// ...
};
// In the .cpp file:
void CClipboardT1Dlg::DoDataExchange(CDataExchange *pDX)
{
CDialog::DoDataExchange(pDX);
// Bind the string variable to the control
DDX_Text(pDX, IDC_NAME, m_strName);
}
Ok, now how to use that?
In a modal dialog it's very easy (assuming m_strName is public):
That's because the data is automatically updated when creating the dialog and when the user hits ok.Code:CClipboardT1Dlg dlg;
dlg.m_strName = strInitialValue;
INT_PTR res = dlg.DoModal();
if(res == IDOK)
{
AfxMessageBox(dlg.m_strName);
}
Another possibility is to do such things in the dialog. Then you'll have to call UpdateData yourself:
As simple as that.Code:// Assuming this function is bound to a button
// click event via the message map:
void CClipboardT1Dlg::OnCleanupClick()
{
// This function cleans up the string in IDC_NAME
// Get the data
UpdateData();
// Clean up the string.
m_strName.Trim();
// Write back to edit control
UpdateData(FALSE);
}