Change Edit Box content when Button is clicked in mfc
Solution 1
You can set the text of an Edit control (wrapped by the CEdit
class in MFC) by calling the SetWindowText
method, which it inherits from the CWnd
base class.
So then all you need to do is respond to a click event on your button control. You do this by listening for the BN_CLICKED
notification from the appropriate button control within your parent window's OnCommand
method.
Something like:
BOOL CMyDialog::OnCommand(WPARAM wParam, LPARAM lParam)
{
if (HIWORD(wParam) == BN_CLICKED && LOWORD(lParam) == IDC_MYBUTTON)
{
m_Edit.SetWindowText(TEXT("My string"));
}
return CWnd::OnCommand(wParam, lParam);
}
Obtaining and reading a book on MFC would be very helpful. This is fairly basic stuff, but it's a lot to cover in a single answer if you don't already understand the fundamental concepts.
Using the Class Wizard would make this even easier... Invoke it with the Ctrl+W keys and follow the on-screen instructions. You'll end up with something like:
void CMyDialog::OnMyButton()
{
m_Edit.SetWindowText(TEXT("My string"));
}
Solution 2
Once you have trapped the button press, in most cases the easiest way to change text in an Edit Control is:
SetDlgItemText(IDC_EDIT_ID, "Desired Text String")
Where IDC_EDIT_ID
is the ID
of the Edit Control (set in the properties
window)
digvijay
Updated on October 17, 2020Comments
-
digvijay over 3 years
I have an Edit Box and a Button on a dialog. How can I change the content in the edit box runtime as the button is clicked? I have to read a new record from a file and post it in the Edit Box as the Button is clicked and I am using mfc.