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 |
|
|
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
-
In an empty code file, type the following
Importsorusingstatements:[!code-csharpSystem.Windows.Forms.BasicForm#2] [!code-vbSystem.Windows.Forms.BasicForm#2]
-
Declare a class named
Form1that inherits from the Form class:[!code-csharpSystem.Windows.Forms.BasicForm#3] [!code-vbSystem.Windows.Forms.BasicForm#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]
-
Add a
Mainmethod to the class.-
Apply the xref:System.STAThreadAttribute to the C#
Mainmethod 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.) -
Call xref:System.Windows.Forms.Application.EnableVisualStyles%2A to apply operating system styles to your application.
-
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
-
At the .NET Framework command prompt, navigate to the directory you created the
Form1class. -
Compile the form.
-
If you are using C#, type:
csc form1.cs-or- -
If you are using Visual Basic, type:
vbc form1.vb
-
-
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
-
Declare a button control named
button1. -
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.
-
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]
-
Create a method to handle the xref:System.Windows.Forms.Control.Click event for the button.
-
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]
-
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]
-
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]