from Microsoft.com
In .NET development, a Windows GUI application is called a Windows Forms (or Winforms) application. Developing a Windows Forms project with Visual C++ is generally the same as with any other .NET language, such as Visual Basic.NET or 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 New Language Features in Visual C++.
In this procedure, you create a Windows Forms application using several standard controls from the Toolbox. In the finished application, a user can select a date, and a text label shows what date was chosen.
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, 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 in a location, or browse to a directory where you want to save the project.
-
The Windows Forms Designer opens, showing Form1 of the project you created.
To add controls to a form
-
If the Toolbox window is not visible, 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 below the Label control.
-
Drag a Button control to the bottom of the form near the center.
Your form should look something like this:
-
To set properties of forms and controls
-
Select the form by clicking on an empty area on its surface.
-
If the Properties window is not visible, click Properties 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 on it and set its Text property to:
Choose a date:
-
Select the button by clicking on it and set its Text property to:
OK
The form should look similar to this:
Writing Event Handler Code
In this section, you write the code to run when these events occur:
-
A Click event on the Button control
-
A ValueChanged event on the DateTimePicker control
To write code to handle events
-
Double-click on the button to add a button click event handler (the default event for a button is a Click event).
-
An empty event handler method is generated for you in the code view of the form displayed in a tabbed page in the editing area.
-
Press Enter after the opening brace of the button1_Click method and type the code to run when that event occurs:
Application::Exit();
IntelliSense™ displays a list of valid possible choices after you type the scope resolution operator (::). You can select a choice from the list and press Tab, double-click it, or continue typing.
-
Return to the Design view by clicking the Form1.h [Design] tab in the editing area or on the View menu, click Designer.
-
Select the DateTimePicker control by clicking on it.
-
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 on the ValueChanged event to open generate an empty event handler in the Code view.
Note ValueChanged is the default event for the DateTimePicker control, so you could also double-click the DateTimePicker control to generate an empty event handler.
-
Press Enter after the opening brace of the dateTimePicker1_ValueChanged method, then type the 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.
Visual Studio provides several features that simplify typing code:
-
When you type an arrow operator (->), IntelliSense displays valid choices you can select from the list.
-
When you type an opening parenthesis for a method, a tooltip window shows valid arguments for each overload of that method. To view the different overloads, use the UP or DOWN arrow keys.
-
Auto-completion can finish typing a variable name or member from what you have typed. For example, if you type String::Fo and press Ctrl-Spacebar or Tab, Visual Studio will complete typing String::Format for you.
-
To build and run the program
-
On 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 on any error to go directly to the line with that error in the source code.
-
On the Debug menu, click Run without Debugging. The application 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 was selected.
-
You can add more features to this application, such as menus, other forms, and help files. Experiment.