Partager 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. Walkthrough: Creating a Simple Windows Form
Walkthrough: Creating a Simple Windows Form
Walkthrough: Creating a Simple Windows Form
Walkthrough: Creating a Simple Windows Form

Procedure

To create the form

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

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

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

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

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

    1. Apply the STAThreadAttribute to the Main method to specify your Windows Forms application is a single threaded apartment.

    2. Call EnableVisualStyles to give a Windows XP appearance to your application.

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

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

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 /r:system.dll,system.drawing.dll,system.windows.forms.dll

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

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

    Private Sub button1_Click(ByVal sender As Object, _
        ByVal e As EventArgs) Handles button1.Click
    
    button1.Click += new EventHandler(button1_Click);
    
  7. Compile and run the application as described in the previous procedure.

Example

Following code example is the complete example from the previous procedures.

Imports System
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
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());
        }
    }
}

Compiling the Code

  • To compile the code, follow the instructions in the proceeding procedure that describe how to compile and run the application.

See Also

Reference

Form
Control

Other Resources

Changing the Appearance of Windows Forms
Enhancing Windows Forms Applications
Getting Started with Windows Forms