CS3240: Data Structures and Algorithms

How to: Create a Windows Forms Application in .NET with C++

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

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

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

  3. The Windows Forms Designer opens, showing Form1 of the project you created.

    A newly created form

To add controls to a form

  1. If the Toolbox window is not visible, click Toolbox on the View menu.

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

    A form with a Label, DateTimePicker, and Button

To set properties of forms and controls

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

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

  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 on it and set its Text property to:

    Choose a date:

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

    OK

    The form should look similar to this:

    Form with changed labels

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

  1. Double-click on the button to add a button click event handler (the default event for a button is a Click event).

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

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

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

  5. Select the DateTimePicker control by clicking on it.

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

  7. Double-click on the ValueChanged event to open generate an empty event handler in the Code view.

    NoteNote

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

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

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

  2. On the Debug menu, click Run without Debugging. The application 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 was selected.

    Form after selecting a date from DateTimePicker
  4. You can add more features to this application, such as menus, other forms, and help files. Experiment.

© Lynne Grewe