HOW TO:從命令列建立 Windows Form 應用程式

下列程序將說明從命令列建立和執行 Windows Form 應用程式的基本步驟。 Visual Studio 中對這些程序有相當廣泛的支援。 如需詳細資訊,請參閱逐步解說:建立簡單的 Windows Form逐步解說:建立簡單的 Windows Form逐步解說:建立簡單的 Windows Form逐步解說:建立簡單的 Windows Form逐步解說:建立簡單的 Windows Form.

程序

若要建立表單

  1. 在空白的程式碼檔中,輸入下面的 import 或 using 陳述式:

    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. 宣告繼承自 Form 類別的 Form1 類別。

    Public Class Form1
        Inherits Form
    
    public class Form1 : Form
    
  3. 為 Form1 建立預設建構函式。

    您將在後續的程序中,將更多的程式碼加入建構函式。

    Public Sub New()
    
    End Sub 'New
    
    public Form1() {}
    
  4. 將 Main 方法加入類別。

    1. STAThreadAttribute 套用至 Main 方法,以指定 Windows Form 應用程式為單一執行緒 Apartment。

    2. 呼叫 EnableVisualStyles 讓應用程式具有 Windows XP 的外觀。

    3. 建立表單的執行個體並執行它。

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

若要編譯和執行應用程式

  1. 在 .NET Framework 命令提示字元中,巡覽到您建立 Form1 類別的目錄。

  2. 編譯表單。

    • 如果您使用的是 C#,請輸入:csc form1.cs

      -或-

    • 如果您使用的是 Visual Basic,請輸入:vbc form1.vb /r:system.dll,system.drawing.dll,system.windows.forms.dll

  3. 在命令提示字元中輸入:Form1.exe

加入控制項和處理事件

前面的程序步驟已經示範如何建立基本的 Windows Form,並進行編譯和執行。 下面的步驟將為您示範如何建立控制項並將它加入表單,以及處理此控制項的事件。 如需可以加入至 Windows Form 的控制項的詳細資訊,請參閱 Windows Form 控制項

除了了解如何建立 Windows Form 應用程式之外,您還需要了解事件架構的程式設計,以及如何處理使用者的輸入。 如需詳細資訊,請參閱在 Windows Form 中建立事件處理常式處理使用者輸入

若要宣告按鈕控制項和處理它的 Click 事件

  1. 宣告名為 button1 的按鈕控制項。

  2. 在建構函式中,建立按鈕並設定它的 SizeLocationText 屬性。

  3. 將此按鈕加入表單。

    下列程式碼範例將示範如何宣告按鈕控制項。

    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. 建立方法,以處理按鈕的 Click 事件。

  5. 在 Click 事件處理常式中,顯示具有 "Hello World" 訊息的 MessageBox

    下列程式碼範例將示範如何處理按鈕控制項的 Click 事件。

    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. Click 事件與所建立的方法產生關聯。

    下列程式碼範例將示範如何將事件與方法產生關聯。

    Private Sub button1_Click(ByVal sender As Object, _
        ByVal e As EventArgs) Handles button1.Click
    
    button1.Click += new EventHandler(button1_Click);
    
  7. 編譯和執行應用程式,如前面的程序中所述。

範例

下列程式碼範例是前面程序的完整範例。

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

編譯程式碼

  • 若要編譯程式碼,請依照前面程序中說明如何編譯和執行應用程式的指示。

請參閱

參考

Form

Control

其他資源

變更 Windows Form 的外觀

增強 Windows Form 應用程式

Windows Form 使用者入門