Files
docs-desktop/dotnet-desktop-guide/framework/winforms/how-to-create-a-windows-forms-application-from-the-command-line.md
T
Andy De George c0ba284473 Initial winforms content migrated (#18)
* Merge winforms framework content to working branch (#14)

* Breadcrumb / TOC / Move net5 to net folder (#15)

* change path from net5 to net

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Add .net 5 winforms placeholder article (#16)

* added some metadata and adjusted net5 placeholder

* corrections

* corrections

* corrections

* Test1

* Swapping landing page vs concept

* fix links

* Fix links

* Fix desc
2020-09-01 16:26:21 -07:00

6.6 KiB

title, titleSuffix, description, ms.date, dev_langs, helpviewer_keywords, ms.assetid
title titleSuffix description ms.date dev_langs helpviewer_keywords ms.assetid
Create a Windows Forms application from the command line Learn how to complete the basic steps to create and run a Windows Forms application from the command line. 03/14/2018
csharp
vb
Windows Forms, application development from command line
Windows Forms, getting started
Windows Forms, creating basic form
45ad3f8b-1c26-4c9f-91a9-3bb0759a47a4

How to: Create a Windows Forms application from the command line

The following procedures describe the basic steps that you must complete to create and run a Windows Forms application from the command line. There is extensive support for these procedures in Visual Studio. Also see Walkthrough: Hosting a Windows Forms Control in WPF.

Procedure

To create the form

  1. In an empty code file, type the following Imports or using statements:

    [!code-csharpSystem.Windows.Forms.BasicForm#2] [!code-vbSystem.Windows.Forms.BasicForm#2]

  2. Declare a class named Form1 that inherits from the Form class:

    [!code-csharpSystem.Windows.Forms.BasicForm#3] [!code-vbSystem.Windows.Forms.BasicForm#3]

  3. Create a parameterless constructor for Form1.

    You will add more code to the constructor in a subsequent procedure.

    [!code-csharpSystem.Windows.Forms.BasicForm#4] [!code-vbSystem.Windows.Forms.BasicForm#4]

  4. Add a Main method to the class.

    1. Apply the xref:System.STAThreadAttribute to the C# Main method to specify your Windows Forms application is a single-threaded apartment. (The attribute is not necessary in Visual Basic, since Windows forms applications developed with Visual Basic use a single-threaded apartment model by default.)

    2. Call xref:System.Windows.Forms.Application.EnableVisualStyles%2A to apply operating system styles to your application.

    3. Create an instance of the form and run it.

    [!code-csharpSystem.Windows.Forms.BasicForm#5] [!code-vbSystem.Windows.Forms.BasicForm#5]

To compile and run the application

  1. At the .NET Framework command prompt, navigate to the directory you created the Form1 class.

  2. Compile the form.

    • If you are using C#, type: csc form1.cs

      -or-

    • If you are using Visual Basic, type: vbc form1.vb

  3. At the command prompt, type: Form1.exe

Adding a control and handling an event

The previous procedure steps demonstrated how to just create a basic Windows Form that compiles and runs. The next procedure will show you how to create and add a control to the form, and handle an event for the control. For more information about the controls you can add to Windows Forms, see Windows Forms Controls.

In addition to understanding how to create Windows Forms applications, you should understand event-based programming and how to handle user input. For more information, see Creating Event Handlers in Windows Forms, and Handling User Input

To declare a button control and handle its click event

  1. Declare a button control named button1.

  2. In the constructor, create the button and set its xref:System.Windows.Forms.Control.Size%2A, xref:System.Windows.Forms.Control.Location%2A and xref:System.Windows.Forms.Control.Text%2A properties.

  3. Add the button to the form.

    The following code example demonstrates how to declare the button control:

    [!code-csharpSystem.Windows.Forms.FormWithButton#2] [!code-vbSystem.Windows.Forms.FormWithButton#2]

  4. Create a method to handle the xref:System.Windows.Forms.Control.Click event for the button.

  5. In the click event handler, display a xref:System.Windows.Forms.MessageBox with the message, "Hello World".

    The following code example demonstrates how to handle the button control's click event:

    [!code-csharpSystem.Windows.Forms.FormWithButton#3] [!code-vbSystem.Windows.Forms.FormWithButton#3]

  6. Associate the xref:System.Windows.Forms.Control.Click event with the method you created.

    The following code example demonstrates how to associate the event with the method.

    [!code-csharpSystem.Windows.Forms.FormWithButton#4] [!code-vbSystem.Windows.Forms.FormWithButton#4]

  7. Compile and run the application as described in the previous procedure.

Example

The following code example is the complete example from the previous procedures:

[!code-csharpSystem.Windows.Forms.FormWithButton#1] [!code-vbSystem.Windows.Forms.FormWithButton#1]

See also