Initialize an Edit Control

Bryan Kelly 361 Reputation points
2024-03-02T21:19:03.95+00:00

Windows 11, VS 2022, C++, MFC

What are the steps to set an initial value for Edit Control items?

Hopefully, an image of the dialog will be shown somewhere in this question. I tried for the bottom after all the text. Now that its here I don't know how to move it. dialog_1

An initial dialog has been created as shown.  Right click on any of the edit control items, containing the text “Sample edit box” and select properties.  The dialog does not show the properties for the selected edit control.  It shows the properties for the button titled Quadratic Equation.

Oops, while writing this, and going back to VS to get the question exactly right, something has changed.  Now it shows the selected Edit Control.  But I cannot change the name of the edit control.  Earlier I was able to change the name but cannot get back to that situation again.  Still, in the properties dialog, there is no field for the initial text of the control item.

1.       What are the steps to change the initial value of the edit control from “Sample edit box” to, for example, 1.0.  

2.       Can the control be set to numeric values only?

3.       At the top of the VS dialog is a Search tool.  I selected it and entered “Sample” in the hopes of finding the code that sets the initial values of those edit controls.  It goes through some gyrations and opens a dialog titled: Visual Studio Installer. 

4.       What??!! Close that and return to this question.

5.       Use keystrokes ctl-f and enter “Sample”  In that dialog the option “Look in” is set to Entire Solution.  The button Find Next returns “The following specified text was not found: Sample”

6.       Again, What??!!  The text is right there in my VS dialog. It must be in the project somewhere.

And yes, I have done multiple searches for subjects such as “vs mfc set initial value of edit control”  Please tell me a good search phrase to find some tips on doing this.

Thank you for your patience.

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,685 questions
{count} votes

2 answers

Sort by: Most helpful
  1. David Lowndes 2,530 Reputation points MVP
    2024-03-02T23:29:38.9066667+00:00
    1. "Sample edit box" is just something displayed by the dialog editor. If you want to populate the control you do so from code. As this is MFC you'd normally do so via DDX - Dialog Data Exchange.
    2. To set the edit control to number only, set the Number property to true.

  2. Minxin Yu 11,506 Reputation points Microsoft Vendor
    2024-03-04T02:55:36.9166667+00:00

    Hi, @Bryan Kelly

    In addition to using the class wizard to bind controls, you can also use GetDlgItem.
    Right click the edit control -> properties to get its id. such as IDC_EDIT1.
    User's image

    BOOL CMFCApplication1Dlg::OnInitDialog()
    {
    ....
    ....
    // TODO: Add extra initialization here
    	CString newValue = _T("New Value"); 
    	CEdit* pEditCtrl = (CEdit*)this->GetDlgItem(IDC_EDIT1); 
    	if (pEditCtrl)
    	{
    		pEditCtrl->SetWindowText(newValue); 
    	}
    	return TRUE;  // return TRUE  unless you set the focus to a control
    }
    

    Best regards,

    Minxin Yu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.