共用方式為


操作說明:從命令列建立 Windows Forms 應用程式

下列程序說明若要從命令列建立及執行 Windows Forms 應用程式,所必須完成的基本步驟。 在 Visual Studio 中,對這些程序有廣泛的支援。 請參閱逐步解說:在 WPF 中裝載 Windows Forms 控制項

程序

建立表單

  1. 在空的程式碼檔案中,輸入下列 Importsusing 陳述式:

    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;
    
    Imports System.ComponentModel
    Imports System.Drawing
    Imports System.Windows.Forms
    
  2. 宣告繼承自表單類別且名為 Form1 的類別:

    public class Form1 : Form
    
    Public Class Form1
        Inherits Form
    
  3. 建立 Form1 的無參數建構函式。

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

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

    1. STAThreadAttribute 套用至 C# Main 方法,以指定您的 Windows Forms 應用程式是單一執行緒 Apartment。 (Visual Basic 中不需要 屬性,因為 Windows Forms 應用程式預設會使用單一執行緒 Apartment 模型來開發。)

    2. 呼叫 EnableVisualStyles 以將作業系統樣式套用至您的應用程式。

    3. 建立表單的執行個體,並加以執行。

    [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
    

編譯和執行應用程式

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

  2. 編譯表單。

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

      -or-

    • 如果您使用 Visual Basic,請輸入:vbc form1.vb

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

加入控制項和處理事件

先前的程序步驟示範只是如何建立可編譯和執行的基本 Windows Form。 下一個程序將會說明如何建立控制項並將其加入表單,以及處理控制項的事件。 如需您可以加入 Windows Forms 之控制項的相關資訊,請參閱 Windows Forms 控制項

除了了解如何建立 Windows Forms 應用程式,您還應該了解以事件為基礎的程式設計,以及如何處理使用者輸入。 如需詳細資訊,請參閱在 Windows Forms 中建立事件處理常式,以及處理使用者輸入

宣告按鈕控制項及處理其 Click 事件

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

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

  3. 將按鈕加入表單中。

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

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

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

    下列程式碼範例會示範如何處理按鈕控制項的點選事件:

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

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

    button1.Click += new EventHandler(button1_Click);
    
    Private Sub button1_Click(ByVal sender As Object, _
        ByVal e As EventArgs) Handles button1.Click
    
  7. 依照先前程序的說明,編譯及執行應用程式。

範例

下列程式碼範例是先前程序的完整範例:

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

另請參閱