Поделиться через


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.

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

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

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

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

  3. The Windows Forms Designer opens, displaying Form1 of the project that you created, as shown here:

    A newly created form

To add controls to a form

  1. If you cannot see the Toolbox window, click Toolbox on the View menu.

  2. Place three controls from the Toolbox on the Form1 design surface:

    1. Drag a Label control to near 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.

    Your form should resemble this:

    A form with a Label, DateTimePicker, and Button

To set properties of forms and controls

  1. Select the form by clicking an empty area on its surface.

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

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

  4. Select the label by clicking it and set its Text property to

    Choose a date:.

  5. Select the button by clicking it and set its Text property to

    OK.

    The form should resemble this:

    Form with changed labels

Writing Event Handler Code

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

To write code to handle events

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

  2. 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();

  3. Return to the Design view by clicking the Form1.h [Design] tab in the editing area or on the View menu, and click Designer.

  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

    ValueChanged is the default event for the DateTimePicker control. Therefore you could also double-click the DateTimePicker control to generate an empty event handler.

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

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

  2. From the Debug menu, click Run without Debugging. The application that you built is displayed.

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

    Form after selecting a date from DateTimePicker

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

Visual C++ Guided Tour

Reference

System.Windows.Forms

Concepts

Overview of Windows-based Applications

Other Resources

Creating Windows-Based Applications