Bewerken

Delen via


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:

    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;
    
    Imports System.ComponentModel
    Imports System.Drawing
    Imports System.Windows.Forms
    
  2. Declare a class named Form1 that inherits from the Form class:

    public class Form1 : Form
    
    Public Class Form1
        Inherits Form
    
  3. Create a parameterless constructor for Form1.

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

    public Form1() {}
    
    Public Sub New()
    
    End Sub
    
  4. Add a Main method to the class.

    1. Apply the 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 EnableVisualStyles to apply operating system styles to your application.

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

    [STAThread]
    public static void Main()
    {
      Application.EnableVisualStyles();
      Application.Run(new Form1());
    }
    
    
        Public Shared Sub Main()
            Application.EnableVisualStyles()
            Application.Run(New Form1())
    
        End Sub
    End Class
    

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 Size, Location and Text properties.

  3. Add the button to the form.

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

    public Button button1;
    public Form1()
    {
        button1 = new Button();
        button1.Size = new Size(40, 40);
        button1.Location = new Point(30, 30);
        button1.Text = "Click me";
        this.Controls.Add(button1);
        button1.Click += new EventHandler(button1_Click);
    }
    
    Public WithEvents button1 As Button
    
    Public Sub New()
        button1 = New Button()
        button1.Size = New Size(40, 40)
        button1.Location = New Point(30, 30)
        button1.Text = "Click me"
        Me.Controls.Add(button1)
       
    End Sub
    
  4. Create a method to handle the Click event for the button.

  5. In the click event handler, display a MessageBox with the message, "Hello World".

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

    private void button1_Click(object sender, EventArgs e)
    {
        MessageBox.Show("Hello World");
    }
    
    Private Sub button1_Click(ByVal sender As Object, _
        ByVal e As EventArgs) Handles button1.Click
        MessageBox.Show("Hello World")
    End Sub
    
  6. Associate the Click event with the method you created.

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

    button1.Click += new EventHandler(button1_Click);
    
    Private Sub button1_Click(ByVal sender As Object, _
        ByVal e As EventArgs) Handles button1.Click
    
  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:

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace FormWithButton
{
    public class Form1 : Form
    {
        public Button button1;
        public Form1()
        {
            button1 = new Button();
            button1.Size = new Size(40, 40);
            button1.Location = new Point(30, 30);
            button1.Text = "Click me";
            this.Controls.Add(button1);
            button1.Click += new EventHandler(button1_Click);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Hello World");
        }
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.Run(new Form1());
        }
    }
}
Imports System.ComponentModel
Imports System.Drawing
Imports System.Windows.Forms

Public Class Form1
    Inherits Form
    Public WithEvents button1 As Button

    Public Sub New()
        button1 = New Button()
        button1.Size = New Size(40, 40)
        button1.Location = New Point(30, 30)
        button1.Text = "Click me"
        Me.Controls.Add(button1)
       
    End Sub
   
    Private Sub button1_Click(ByVal sender As Object, _
        ByVal e As EventArgs) Handles button1.Click
        MessageBox.Show("Hello World")
    End Sub

    <STAThread()> _
    Shared Sub Main()
        Application.EnableVisualStyles()
        Application.Run(New Form1())

    End Sub
End Class

See also