Visual C++
Creating a Windows Forms Application By Using the .NET Framework (C++)

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 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 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. If you are just getting started learning C++, we recommend the "C++ Beginner's Guide," written by Herb Schildt, available online at http://go.microsoft.com/fwlink/?LinkId=115303.

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

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

    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.

  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.

    NoteNote:

    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.

    Visual Studio provides several features that simplify typing code:

    • When you type an arrow operator (->), IntelliSense displays valid choices that 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. 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 Applications (C++) | Next: Creating a Windows Forms Control (C++)

See Also

Tasks

Concepts

Reference

Other Resources

Tags :


Community Content

Thomas Lee
Good Form App Tutorial, But
Anyone figure out what the OK button does? Did I miss something?

[tfl - 02 08 09] Hi - and thanks for your post. You should post questions like this to the MSDN Forums at http://forums.microsoft.com/msdn or the MSDN Newsgroups at http://www.microsoft.com/communities/newsgroups/en-us/. You are much more likely get a quicker response using the forums than through the Community Content. For specific help about:
Visual Studio :
http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.vstudio%2C&
SQL Server :
http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.sqlserver%2C&
.NET Framework :
http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.dotnet.framework
PowerShell : http://groups.google.com/group/microsoft.public.windows.powershell/topics?pli=1
All Public : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public%2C&

_baeltazor
but why?
why is it that everytime someone posts a question related to the article they are told to post it on a different forum? and why are these code articles always missing something?
Tags :

Page view tracker