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.

link to video 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

  1. On the File menu, click New, and then click Project.

  2. 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.

  3. Form1 of the project opens in the Windows Forms Designer opens, as shown in the following picture.

    A newly created form

To add controls to a form

  • Drag three controls from the Toolbox to Form1, as follows.

    1. Drag a Label control to the top-left corner of Form1.

    2. Drag a DateTimePicker control just under the Label control.

    3. Drag a Button control to the bottom of the form, near the center.

    The form should resemble the following picture.

    A form with a Label, DateTimePicker, and Button

To set the properties of forms and controls

  1. Right-click an empty area of the form and then click Properties.

  2. Set the Text property to Date Chooser.

    This text is displayed in the title bar of the form.

  3. Select the label and set its Text property to Choose a date:.

  4. Select the button and set its Text property to OK.

    The form should resemble the following picture.

    Form with changed labels

Writing Event Handler Code

In this section, you write the code that is to run when these events occur:

To write code to handle events

  1. 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).

  2. 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();
    
  3. Return to the Design view by clicking the Form1.h [Design] tab in the editing area.

  4. Click the DateTimePicker control.

  5. 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.

  6. 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.

  7. 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

  1. 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.

  2. On the Debug menu, click Run without Debugging to start the application.

  3. 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.

    Form after selecting a date from DateTimePicker

  4. 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

Visual C++ Guided Tour

Concepts

Overview of Windows-based Applications

Reference

System.Windows.Forms

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.