Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2005
Visual Studio
Windows Forms
 How to: Create a Windows Forms Appl...
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
Windows Forms Programming 
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. For more information, see Walkthrough: Creating a Simple Windows Form.

Procedure

To create the form

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

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

    Visual Basic
    Public Class Form1
        Inherits Form
    
    C#
    public class Form1 : Form
    
  3. Create a default constructor for Form1.

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

    Visual Basic
    Public Sub New()
    
    End Sub 'New
    
    C#
    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.

    Visual Basic
        <STAThread()> _
        Public Shared Sub Main()
            Application.EnableVisualStyles()
            Application.Run(New Form1())
    
        End Sub
    End Class
    
    C#
    [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.

    Visual Basic
    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
    
    C#
    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.

    Visual Basic
    Private Sub button1_Click(ByVal sender As Object, _
        ByVal e As EventArgs) Handles button1.Click
        MessageBox.Show("Hello World")
    End Sub
    
    C#
    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.

    Visual Basic
    Private Sub button1_Click(ByVal sender As Object, _
        ByVal e As EventArgs) Handles button1.Click
    
    C#
    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.

Visual Basic
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
C#
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

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker