Creating a Windows Forms Application By Using the .NET Framework (C+)
In .NET development, a Windows application that has a graphical user interface (GUI) is typically a Windows Forms application. Development of a Windows Forms project by using Visual C++ generally resembles development by using any other .NET language, for example, Visual Basic or C#.
Windows Forms applications in Visual C++ use the .NET Framework classes and other .NET features together with the new Visual C++ syntax. For more information, see Language Features for Targeting the CLR.
This document shows how to 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 that date.
Prerequisites
You must 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+).
To create a Windows Forms project
On the File menu, click New, and then click Project.
In the Project Types pane, click Visual C++ and then click CLR. In the Templates pane, click Windows Forms Application.
Type a name for the project, for example, winformsapp. Specify the directory where you want to save the project.
Form1 of the project opens in the Windows Forms Designer opens, as shown in the following picture.
To add controls to a form
Drag three controls from the Toolbox to Form1, as follows.
Drag a Label control to 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.
The form should resemble the following picture.
To set the properties of forms and controls
Right-click an empty area of the form and then click Properties.
Set the Text property to Date Chooser.
This text is displayed in the title bar of the form.
Select the label and set its Text property to Choose a date:.
Select the button and set its Text property to OK.
The form should resemble the following picture.
Writing Event Handler Code
In this section, you write the code that is 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 Click event handler. (The default event for a button is a Click event.)
This action generates an empty event handler method. The related code is displayed on the Code view tab of the editor.
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).
On a new line just after the opening brace of the button1_Click method, type the following code to be run when that event occurs.
Application::Exit();
Return to the Design view by clicking the Form1.h [Design] tab in the editing area.
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
Because ValueChanged is the default event for the DateTimePicker control, you could also double-click the DateTimePicker control to generate an empty event handler.
On a new line just after the opening brace of the dateTimePicker1_ValueChanged method, type the following code to be 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:" and the Text property of the DateTimePicker is appended to that string.
Visual Studio provides the following features to make typing code easier:
When you type an arrow operator (->), IntelliSense displays valid choices that you can select from.
When you type an opening parenthesis for a method, a list of valid arguments for each overload of that method is displayed. To view the different overloads, press the UP ARROW or DOWN ARROW key.
Auto-completion can finish a partially typed variable name or member name. For example, if you type String::Fo and then press CTRL+SPACEBAR or TAB, Visual Studio completes the entry as String::Format.
To build the application and run it
On the Build menu, click Build Solution.
If there are any errors, in the Output window, click Go to Next Message. The error message text appears in the status bar. You can double-click any error to go to the line in the source code where the error occurs.
On the Debug menu, click Run without Debugging to start the application.
Test the application by clicking the arrow next to the date-picker box and then selecting a date. The label text changes to show the date that you selected, as shown in the following picture.
You can add more features to this application, for example, menus, other forms, and Help files. Do not be afraid to experiment.
Next Steps
Previous:Creating Win32 Applications (C+) | Next:Creating a Windows Forms Control (C+)
See Also
Tasks
Concepts
Overview of Windows-based Applications
Reference
Other Resources
Creating Windows-Based Applications
Change History
Date |
History |
Reason |
---|---|---|
January 2010 |
Added a note that describes other code that is generated. |
Customer feedback. |