2-Windows 窗體應用程序模型

Windows 窗體的應用程序編程模型主要由窗體、控件及其事件組成。MDI 窗體。MDI 窗體可以在其工作區內包含名爲 MDI 子窗體的其他窗體。Form 類爲鍵盤處理(Tab 鍵順序)和滾動窗體的內容提供內置的支持。

 窗體
在 Windows 窗體中,Form 類是在應用程序中顯示的任何窗口的表示形式。可以使用 Form 類的 BorderStyle 屬性創建標準窗口、工具窗口、無邊框窗口和浮動窗口。還可使用 Form 類創建有模式窗口,如對話框。通過設置 Form 類的 MDIContainer 屬性,可以創建一種特殊類型的窗體 MDI 窗體。MDI 窗體可以在其工作區內包含名爲 MDI 子窗體的其他窗體。Form 類爲鍵盤處理(Tab 鍵順序)和滾動窗體的內容提供內置的支持。

當爲應用程序設計用戶界面時,通常創建一個從 Form 派生的類。然後可以添加控件、設置屬性、創建事件處理程序以及向窗體添加編程邏輯。

控件
添加到窗體中的每個組件(如 Button、TextBox 或 RadioButton)稱爲控件。Windows 窗體包括通常與 Windows 關聯的所有控件以及類似 Windows 窗體 DataGrid 的自定義控件。

通常可以通過設置屬性與控件進行交互,以更改其外觀和行爲。例如,下面的 Form 的派生類向窗體添加一個 Button 控件,並設置該控件的 Size 和 Location。

C# source:

public class HelloWorldForm : System.Windows.Forms.Form {

    private Button button1 = new Button() ;
    private TextBox textBox1 = new TextBox();

    [STAThread]
    public static int Main(string[] args) {
        Application.Run(new HelloWorldForm());
        return 0;
    }

    public HelloWorldForm() {

        this.Text = "Hello Windows Forms World";
        this.AutoScaleBaseSize = new Size(5, 13);
        this.ClientSize = new Size(392, 117);

        this.MinimumSize = new Size(392, (117 + SystemInformation.CaptionHeight));

        this.AcceptButton=button1;

        button1.Location = new Point(256, 64);
        button1.Size = new Size(120, 40);
        button1.TabIndex = 2;
        button1.Text = "Click Me!";

        button1.Click += new System.EventHandler(button1_Click);

        textBox1.Text = "Hello Windows Forms World";
        textBox1.TabIndex = 1;
        textBox1.Size = new Size(360, 20);
        textBox1.Location = new Point(16, 24);

        this.Controls.Add(button1);
        this.Controls.Add(textBox1);
    }
}

VB source:


Public Class HelloWorldForm
    Inherits Form

    Private components As Container
    Private WithEvents button1 as Button
    Private textBox1 As New TextBox

    <STAThread()> Shared Sub Main()
        System.Windows.Forms.Application.Run(New HelloWorldForm())
    End Sub

    Public Sub New()
        MyBase.New

        Me.Text = "Hello Windows Forms World"
        Me.AutoScaleBaseSize = new Size(5, 13)
        Me.ClientSize = new Size(392, 117)

        Me.MinimumSize = new Size(392, (117 + SystemInformation.CaptionHeight))

        button1 = new Button()
        button1.Location = new Point(56, 64)
        button1.Size = new Size(90, 40)
        button1.TabIndex = 2
        button1.Text = "Click Me!"

        textBox1.Text = "Hello Windows Forms World"
        textBox1.TabIndex = 1
        textBox1.Size = new Size(360, 20)
        textBox1.Location = new Point(16, 24)

        Me.AcceptButton=button1

        Me.Controls.Add(button1)
        Me.Controls.Add(textBox1)
    End Sub
End Class

 

控件狀態爲無模式
窗體對於何時可設置控件的屬性提供有限的限制。控件沒有阻止更新其狀態的模式。創建控件的新實例後,可以立即更改其狀態。例如,下面的代碼提供兩個示例,演示創建 Button 控件的有效方法。

C# source:
Button button1 = new Button();
button1.Location = new Point(256, 64);
button1.Size = new Size(120, 40);
button1.TabIndex = 1;
button1.Text = "Click Me!";
this.Controls.Add(button1);

VB source:
Dim button1 As New Button
button1.Location = New Point(256, 64)
button1.Size = New Size(120, 40)
button1.TabIndex = 1
button1.Text = "Click Me!"
Me.Controls.Add(button1)

事件
Windows 窗體編程模型基於事件。當控件更改狀態,如當用戶單擊按鈕時,它引發一個事件。爲了處理事件,應用程序爲該事件註冊一個事件處理方法。 在 Visual Basic 中,有兩種途徑可以註冊事件處理方法:

如果使用 WithEvents 關鍵字聲明控件變量,可以在方法的聲明中使用 Handles 關鍵字,將該方法註冊爲事件處理方法。 可使用 AddHandler 在運行時註冊事件處理方法。
下面的代碼闡釋註冊事件處理方法的兩種途徑。

C# source:
public delegate void EventHandler(object sender, EventArgs e);
VB source:
Public Delegate Sub EventHandler(sender As object, e As EventArgs)

 Click 事件的任何事件處理方法都必須具有以下簽名。

C# source:
<access> void <name>(object sender, EventArgs evArgs)
VB source:
<access> Sub <name>(sender As object, e As EventArgs)

 對於強類型語言,如果事件處理方法的簽名與委託簽名不匹配,將發生編譯時錯誤。

很多事件使用一般的 EventHandler 和 EventArgs 類。但是,一些事件要求針對所引發事件的類型的附加信息。例如,鼠標移動事件包括有關鼠標指針或鼠標按鈕位置的信息。這些事件定義其自己的類,這些類必須從 EventHandler 和 EventArgs 類繼承。例如,MouseDown 事件使用 MouseEventHandler 和 MouseEventArgs 類。

事件命名約定
可以在特定種類的狀態更改之前和之後引發事件。在狀態更改前引發的事件通常帶有後綴“ing”。在狀態更改後引發的事件通常帶有後綴“ed”。例如,SessionEnding 事件是狀態更改前引發的,SessionEnded 事件是狀態更改後引發的。如果某狀態更改僅導致一個事件被引發,則該事件通常沒有後綴。例如,Click 事件。

可取消的事件
根據應用程序中的情況,可能需要取消某個事件。某些事件可以取消。這些事件使用 CancelEventHandler 和 CancelEventArgs 類。CancelEventArgs 類包含名爲 Cancel 的屬性。如果此屬性設置爲 true,那麼當該事件處理方法返回時,將取消該事件。通常,只有在狀態更改前引發的事件纔是可以取消的。取消事件將取消狀態更改。

用一個事件處理方法處理多個事件
如果要用一個事件處理程序處理多個事件,可通過將同一方法註冊到多個事件來實現。每個事件都必須具有相同的簽名。當對多個事件使用一個事件處理方法時,可以從 sender 參數確定哪個控件引發了事件。下面的示例闡釋處理來自兩個按鈕控件的事件的單個事件處理方法。

C# source:
....
Button button1 = new Button() ;
Button button2 = new Button() ;
....
button1.Click += new System.EventHandler(button_Click);
button2.Click += new System.EventHandler(button_Click);
....

//The event handling method
private void button_Click(object sender, EventArgs evArgs) {
    if (sender==button1) {
        MessageBox.Show("Button1 Pushed!");
    } else if (sender==button2) {
        MessageBox.Show("Button2 Pushed!");
    }
}

VB source:
....
Dim button1 As New Button
Dim button2 As New Button
....
AddHandler button1.Click, AddressOf button_Click
AddHandler button2.Click, AddressOf button_Click
....

'The event handling method
Private Sub button_Click(sender As Object, evArgs As EventArgs)
    If (sender = button1) Then
        MessageBox.Show("Button1 Pushed!")
    Else If (sender = button2) Then
        MessageBox.Show("Button2 Pushed!")
    End If
End Sub

 

下面的示例演示前面提到的概念。它顯示如何:
創建和顯示一個作爲應用程序主入口點的窗體
向窗體添加控件
設置控件上的屬性
將事件處理方法註冊到控件
從控件獲取屬性值
在消息框中顯示文本
向關閉窗體時執行的 Dispose 方法添加代碼

VB source:

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

Namespace Microsoft.Samples.WinForms.VB.HelloWorldForm
    Public Class HelloWorldForm
        Inherits Form

        Private components As Container

        '使用 WithEvents 聲明按鈕,以便能夠註冊聲明性事件處理程序
        Private WithEvents button1 as Button

        Private textBox1 As New TextBox

        '應用程序啓動時顯示此窗體
        <STAThread()> Shared Sub Main()
            System.Windows.Forms.Application.Run(New HelloWorldForm())
        End Sub

        Public Sub New()

            MyBase.New

            '設置窗體
            Me.Text = "Hello Windows Forms World"
            Me.AutoScaleBaseSize = new Size(5, 13)
            Me.ClientSize = new Size(392, 117)

            '將最小窗體大小設置爲工作區的大小,高度與標題欄相同
            Me.MinimumSize = new Size(392, (117 + SystemInformation.CaptionHeight))

            '創建 button1
            button1 = new Button()
            button1.Location = new Point(56, 64)
            button1.Size = new Size(90, 40)
            button1.TabIndex = 2
            button1.Text = "請單擊我!"

            '創建文本框
            textBox1.Text = "Hello Windows Forms World"
            textBox1.TabIndex = 1
            textBox1.Size = new Size(360, 20)
            textBox1.Location = new Point(16, 24)

            '在窗體上設置默認按鈕
            Me.AcceptButton=button1

            '向窗體添加控件
            Me.Controls.Add(button1)
            Me.Controls.Add(textBox1)

        End Sub

        '當窗體關閉時調用
        '注意,MessageBox 調用只是說明調用了 Dispose。
        '應使 Dispose 方法代碼儘可能地簡單可靠
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            Try
                MessageBox.Show("已處置!")
            Catch ex As Exception
            End Try
            MyBase.Dispose(disposing)
        End Sub



        'button2 的事件處理方法 - 使用 Handles 註冊
        Private Sub button1_Click(sender As Object, evArgs As EventArgs) Handles button1.Click

            '禁用 button1 - 我們只想添加一個按鈕
            button1.Enabled=False

            '添加新按鈕,並使用 AddHandler 添加事件處理程序
            Dim newButton As new Button
            newButton = new Button()
            newButton.Location = new Point(256, 64)
            newButton.Size = new Size(90, 40)
            newButton.TabIndex = 4
            newButton.Text = "還請單擊我!"
            Me.Controls.Add(newButton)

            AddHandler newButton.Click, AddressOf Me.clickNewbutton
        End Sub

        '新按鈕的事件處理方法 - 使用 AddHandler 註冊
        Private Sub clickNewbutton(sender As Object, evArgs As EventArgs)
            MessageBox.Show("來自新按鈕的問候")
        End Sub

    End Class

End Namespace

C# source:
namespace Microsoft.Samples.WinForms.Cs.HelloWorldForm {
    using System;
    using System.Windows.Forms;
    using System.Drawing;

    // 不要向 Windows 窗體設計器中加載此窗體。
    // 此“快速入門”是包含控件的簡單手寫
    // 窗體的一個示例。
    public class HelloWorldForm : System.Windows.Forms.Form {

        private Button button1 = new Button() ;
        private TextBox textBox1 = new TextBox();

        //應用程序啓動時顯示此窗體
        [STAThread]
        public static int Main(string[] args) {
            Application.Run(new HelloWorldForm());
            return 0;
        }

        public HelloWorldForm() {

            //設置窗體
            this.Text = "Hello Windows Forms World";
            this.AutoScaleBaseSize = new Size(5, 13);
            this.ClientSize = new Size(392, 117);

            //將最小窗體大小設置爲工作區的大小,高度與標題欄相同
            this.MinimumSize = new Size(392, (117 + SystemInformation.CaptionHeight));

            //在窗體上設置默認按鈕
            this.AcceptButton=button1;

            //創建按鈕
            button1.Location = new Point(256, 64);
            button1.Size = new Size(120, 40);
            button1.TabIndex = 2;
            button1.Text = "請單擊我!";

            //註冊事件處理程序
            button1.Click += new System.EventHandler(button1_Click);

            //創建文本框
            textBox1.Text = "Hello Windows Forms World";
            textBox1.TabIndex = 1;
            textBox1.Size = new Size(360, 20);
            textBox1.Location = new Point(16, 24);

            //向窗體添加控件
            this.Controls.Add(button1);
            this.Controls.Add(textBox1);
        }

        //當窗體關閉時調用
        //注意,MessageBox 調用只是
        //說明調用了 Dispose。
        //應使 Dispose 方法代碼儘可能地
        //簡單可靠
        protected override void Dispose(bool disposing)
        {
            try {
                MessageBox.Show("已處置!");
            } catch(Exception) {}
            base.Dispose(disposing);
        }

        //事件處理方法
        private void button1_Click(object sender, EventArgs evArgs) {
            MessageBox.Show("文本是:'" + textBox1.Text + "'");
        }
    }
}









 
發佈了60 篇原創文章 · 獲贊 2 · 訪問量 15萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章