Click to See Complete Forum and Search --> : Change Text in a control? NewB
JasonLpz
Jan 19th, 2003, 04:14 PM
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?
JasonLpz
Jan 19th, 2003, 04:32 PM
I guess i should have searched harder i learned it
Here (http://www.vbforums.com/showthread.php?s=&threadid=79643&highlight=mfc+edit) :
CClipboardT1Dlg::SetDlgItemText(IDC_EDIT1,"Jason");
CornedBee
Jan 20th, 2003, 03:41 AM
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.
JasonLpz
Jan 20th, 2003, 08:36 AM
What is DDX and how do i create a control variable?
CornedBee
Jan 21st, 2003, 08:21 AM
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.
JasonLpz
Jan 21st, 2003, 09:12 AM
that sounds so confusing. and complicated i rather do it my way but thanks alot ill look into it
CornedBee
Jan 21st, 2003, 10:03 AM
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).
CornedBee
Jan 21st, 2003, 10:15 AM
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:
class CClipboardT1Dlg : public CDialog
{
// ...
CString m_strName;
// ...
};
2) Add an override of DoDataExchange (if there isn't already one):
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);
}
Now the variable is bound. What's complicated about that? ;)
Ok, now how to use that?
In a modal dialog it's very easy (assuming m_strName is public):
CClipboardT1Dlg dlg;
dlg.m_strName = strInitialValue;
INT_PTR res = dlg.DoModal();
if(res == IDOK)
{
AfxMessageBox(dlg.m_strName);
}
That's because the data is automatically updated when creating the dialog and when the user hits ok.
Another possibility is to do such things in the dialog. Then you'll have to call UpdateData yourself:
// 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);
}
As simple as that.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.