Export (0) Print
Expand All

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

To create the form

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

    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 : Form
    
  3. Create a default constructor for Form1.

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

    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 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

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);
    }
    
  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");
    }
    
  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);
    
  7. Compile and run the application as described in the previous procedure.

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

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

Community Additions

ADD
Show:
© 2014 Microsoft