共用方式為


在 Windows Form 中使用控制項的概觀

更新:2007 年 11 月

這個主題說明 Windows Form 應用程式的基本項目,並提供使用控制項和在 Windows Form 應用程式中處理事件的簡單範例。

簡單 Windows Form 應用程式

就最低限度,Windows Form 應用程式至少由下列項目組成。

  • 衍生自 System.Windows.Forms.Form 的一或多個類別。

  • 叫用 (Invoke) static (在 Visual Basic 中為 shared) Run 方法並且為其傳遞 Form 執行個體的 Main 方法。Run 方法會處理從作業系統傳到應用程式的訊息。

下列程式碼範例示範 Windows Form 應用程式的基本項目。

Option Explicit
Option Strict

Imports System
Imports System.Windows.Forms

Public Class MyForm
   Inherits Form
   
   Public Sub New()
      Me.Text = "Hello World"
   End Sub 'New
   
   <STAThread()> _
   Public Shared Sub Main()
      Dim aform As New MyForm()
      ' The Application.Run method processes messages from the operating system 
      ' to your application. If you comment out the next line of code, 
      ' your application will compile and execute, but because it is not in the
      ' message loop, it will exit after an instance of the form is created.
      Application.Run(aform)
   End Sub
End Class
using System;
using System.Windows.Forms;

public class MyForm : Form {

   public MyForm() {
      this.Text = "Hello World";
   }
   [STAThread]
   public static void Main(string[] args) {
     MyForm aform = new MyForm();
// The Application.Run method processes messages from the operating system 
// to your application. If you comment out the next line of code, 
// your application will compile and execute, but because it is not in the // message loop, it will exit after an instance of the form is created.
      Application.Run(aform);
   }
}

在 Windows Form 應用程式中使用控制項

下列程式碼範例示範簡單應用程式,說明 Windows Form 應用程式如何使用控制項和處理事件。該範例包含在表單上的三個按鈕;每一個按鈕在被按一下時,都會變更背景色彩。

Option Explicit
Option Strict

Imports System
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Resources
Imports System.Drawing

Public Class MyForm
   Inherits Form
   Private red As Button
   Private blue As Button
   Private green As Button
   
   Public Sub New()
      InitializeComponent()
   End Sub
   
   
   Protected Overloads Overrides Sub Dispose(disposing as Boolean)
      MyBase.Dispose(disposing)
   End Sub
   
   ' InitializeComponent is a helper method for the constructor. 
   ' It is included for consistency with code that is 
   ' auto-generated by the Windows Forms designer in Visual Studio. 
   Private Sub InitializeComponent()
      
      ' Creates three buttons, sets their properties, and attaches
      ' an event handler to each button.
      red = New Button()
      red.Text = "Red"
      red.Location = New Point(100, 50)
      red.Size = New Size(50, 50)
      AddHandler red.Click, AddressOf button_Click
      Controls.Add(red)
      
      blue = New Button()
      blue.Text = "Blue"
      blue.Location = New Point(100, 100)
      blue.Size = New Size(50, 50)
      AddHandler blue.Click, AddressOf button_Click
      Controls.Add(blue)
      
      green = New Button()
      green.Text = "Green"
      green.Location = New Point(100, 150)
      green.Size = New Size(50, 50)
      AddHandler green.Click, AddressOf button_Click
      Controls.Add(green)
   End Sub
   
   ' Event handler.
   Private Sub button_Click(sender As Object, e As EventArgs)
      If sender Is red Then
         Me.BackColor = Color.Red
      Else
         If sender Is blue Then
            Me.BackColor = Color.Blue
         Else
            Me.BackColor = Color.Green
         End If
      End If 
   End Sub

   ' The STAThreadAttribute informs the common language runtime that
   ' Windows Forms uses the single-threaded apartment model.
   <STAThread()> _
   Public Shared Sub Main()
      Application.Run(New MyForm())
   End Sub
End Class
using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Resources;
using System.Drawing;

public class MyForm : Form {
   private Button red;
   private Button blue;
   private Button green;

   public MyForm() : base() {   
      InitializeComponent();   
   }

   protected override void Dispose(bool disposing) {
      base.Dispose(disposing);
   }
   
// InitializeComponent is a helper method for the constructor. 
// It is included for consistency with code that is 
// auto-generated by the Windows Forms designer in Visual Studio. 
   private void InitializeComponent() {
   
// A delegate for the click event of a button. The argument to 
// the constructor contains a reference to the method that performs the 
// event handling logic.
      EventHandler handler = new EventHandler(button_Click);
   
// Creates three buttons, sets their properties, and attaches
// an event handler to each button.

      red = new Button();
      red.Text = "Red";
      red.Location = new Point(100, 50);
      red.Size = new Size(50, 50);
      red.Click +=handler;
      Controls.Add(red);
      
      blue = new Button();
      blue.Text = "Blue";
      blue.Location = new Point(100, 100);
      blue.Size = new Size(50, 50);
      blue.Click += handler;
      Controls.Add(blue);
      
      green = new Button();
      green.Text = "Green";
      green.Location = new Point(100, 150);
      green.Size = new Size(50, 50);
      green.Click += handler;
      Controls.Add(green);      
      }
      
   // Event handler.
   private void button_Click(object sender, EventArgs e) {
            if (sender == red) this.BackColor = Color.Red ;
                  else if (sender == blue) this.BackColor = Color.Blue;
                  else this.BackColor = Color.Green;
        }
   // The STAThreadAttribute informs the common language runtime that
   // Windows Forms uses the single-threaded apartment model.
  [STAThread]
   public static void Main(string[] args) {
   Application.Run(new MyForm());
   }
   
}

請參閱

概念

Windows Form 控制項開發的基本概念

其他資源

使用 .NET Framework 開發自訂的 Windows Form 控制項