Using your last project, delete the calendar control by clicking
on it and pressing delete.
Insert an edit box.
Name it "IDC_EDIT1". (do this by highlighting the edit box, and
pressing Alt-Enter)
Assigning variables to controls
Press Control-W to get the "MFC ClassWizard" window, displaying information
about all the classes used in your window.
Click on the "Member variables" tab. This tab allows you to associate
a variable with each control, either to refer to data in the control,
or to the control itself.
Select "IDC_EDIT1" and click on "Add variable"
You have the choice of a "value" or a "control." Select value,
and type "Cstring" to indicate that we wish to maintain a string
variable representing the text in the edit box.
Type a variable name "m_strFileName" and click OK.
Click OK again to close the ClassWizard. Press F5 again to
compile and test your program.
Creating a button
The next step is to create a "Button", something the user can click
on to achieve a result. Click on "button" in the toolbar, and
once again on your form to create a button.
Right-click on the button, and select "properties"
Give your button the name "IDC_BUTTON1" and the caption "&Files"
Notice how the character after the & becomes underlined.
This is called a hot-key. When you run your application, you
will be able to press Alt-F instead of clicking on this button.
Compile and run your program (F5) It should display a dialog
box with all your controls on it.
Adding code to your button
In the dialog editor, double-click on your button. A box will
appear asking you for the name of a function to call when the user
clicks on the button. Click "OK" to accept the default name
of "OnButton1"
A code window appears, where you can type in C++ code. Any
code you type here will be run each time the user clicks on the
"Files" button.
Replace the "TODO" comment with the following line
MessageBox("Button Clicked");
Add a comment to explain the line of code you've just written.
/* Display a message indicating that the
button was clicked */
MessageBox("Button Clicked");
Press F5 to run your program. When you click on the button,
it will display your message.
Next: Using fancy controls.