Creating a Windows Forms Application By Using the .NET Framework (C++)
Developing a Windows Forms project by using Visual C++ is generally the same as with any other .NET language, such as Visual Basic or Visual C#.
Windows Forms applications in Visual C++ use the .NET Framework classes and other .NET features with the new Visual C++ syntax. For more information, see Language Features for Targeting the CLR.
In this procedure, you create a Windows Forms application by using several standard controls from the Toolbox. In the finished application, a user can select a date, and a text label shows the date that the user chose.
Prerequisites
This topic assumes that you understand the fundamentals of the C++ language.
For a video version of this topic, see Video How to: Creating a Windows Forms Application By Using the .NET Framework (C++).
Note
Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Visual Studio Settings.
To create a new Windows Forms project
On the File menu, click New, and then click Project….
In the Project Types pane, select CLR in the Visual C++ node, and then select Windows Forms Application in the Templates pane.
Type a name for the project, such as winformsapp. You can accept the default location, type a location, or browse to a directory where you want to save the project.
The Windows Forms Designer opens, displaying Form1 of the project that you created, as shown here:
To add controls to a form
If you cannot see the Toolbox window, click Toolbox on the View menu.
Place three controls from the Toolbox on the Form1 design surface:
Drag a Label control to near the top-left corner of Form1.
Drag a DateTimePicker control just under the Label control.
Drag a Button control to the bottom of the form near the center.
Your form should resemble this:
To set properties of forms and controls
Select the form by clicking an empty area on its surface.
If you cannot see the Properties Window, click Properties Window on the View menu (or press F4).
You may want to close the Toolbox for more room.
Set the form's Text property (shown in the form Title Bar) by clicking to the right of the Text property in the Properties Window and typing:
Date Chooser
Select the label by clicking it and set its Text property to
Choose a date:.
Select the button by clicking it and set its Text property to
OK.
The form should resemble this:
Writing Event Handler Code
In this section, you write the code to run when these events occur:
A ValueChanged event on the DateTimePicker control.
To write code to handle events
Double-click the button to add a button click event handler (the default event for a button is a Click event).
This action generates an empty event handler method in the code view of the form displayed in a tabbed page in the editing area.
Note
One line of code is also added to the InitializeComponent function that creates the event handler and assigns it to the Click field that is associated with the control. If you double-click the control in Design view to add the relevant code, and then decide to remove it later, delete both additions (not just the empty event handler).
Move the cursor to after the opening brace of the button1_Click method, press Enter, and type the following code to run when that event occurs:
Application::Exit();
Return to the Design view by clicking the Form1.h [Design] tab in the editing area or on the View menu, and click Designer.
Click the DateTimePicker control.
To add a ValueChanged event handler for the DateTimePicker control, click the lightning bolt icon in the Properties window to display events for that control.
Double-click the ValueChanged event to generate an empty event handler in the Code view.
Note
ValueChanged is the default event for the DateTimePicker control. Therefore you could also double-click the DateTimePicker control to generate an empty event handler.
Move the cursor to after the opening brace of the dateTimePicker1_ValueChanged method, press Enter, and then type the following code to run when the event occurs:
label1->Text=String::Format("New date: {0}", dateTimePicker1->Text);
When a user of the application selects a new date, the Text property of the label is set to the literal string "New date:" with the Text property of the DateTimePicker appended to that string.
To build and run the program
From the Build menu, click Build Solution.
If there are any errors, click the Go to Next Message button in the Output window. The error message text appears in the status bar. You can double-click any error to go to the line with that error in the source code.
From the Debug menu, click Run without Debugging. The application that you built is displayed.
Test the application by clicking the down arrow on the DateTimePicker and selecting a date. The label text changes to show the date that you selected, as shown here:
You can add more features to this application, such as menus, other forms, and Help files. Do not be afraid to experiment.
Next Steps
Previous: Creating Win32-Based Applications (C++) | Next: Creating a Windows Forms Control (C++)
See Also
Tasks
Reference
Concepts
Overview of Windows-based Applications