共用方式為


使用 Windows Form 的 Windows 用戶端

到目前為止,所有範例都是寫入系統主控台的命令列程式。現在,您已了解整個開發程序,將要重新撰寫用戶端應用程式,使用所有 .NET 語言都可以使用的新的 Windows Form 程式庫。範例會使用 Visual Basic,以下就是完整的原始程式碼清單:

清單 1:Visual Basic 中的 Windows Form 用戶端 (ClientWinForms.vb)

Option Explicit
Option Strict

Imports System
Imports System.Collections
Imports System.Drawing
Imports System.Threading
Imports System.Windows.Forms

Imports CompCS
Imports CompVB
Imports CompVC

Public Module modmain
   Public Const vbCrLf as String = 
      Microsoft.VisualBasic.ControlChars.CrLf
   Public Class Client
      Inherits Form

      ' This code is required by the Windows Forms Designer.
      Private components As 
         System.ComponentModel.Container
      Private Button2 As System.Windows.Forms.Button
      Private Button1 As System.Windows.Forms.Button
      Private Label1 As System.Windows.Forms.Label

      Public Sub New()
         MyBase.New
         InitForm  'Required by the Windows Forms Designer.
      End Sub

      'Form overrides Dispose(Boolean) to clean up component list
      '(this Form has no components), then disposes of the base class
      Protected Overloads Overrides Sub Dispose(disposing As Boolean)
            If disposing Then
                  If Not (components Is Nothing) Then 
                        components.Dispose()
                  End If
            End If
            MyBase.Dispose(disposing)
      End Sub 

      ' This is the Main entry point for the application.
      Shared Sub Main()
         Application.Run(New Client)
      End Sub

      ' This procedure is required by the Windows Forms Designer.
      ' It can be modified using the Windows Forms Designer.  
      ' Do not modify it using the code editor.
      Private Sub InitForm()
          Me.Button1 = New Button
          Me.Button2 = New Button
          Me.Label1 = New Label

          Button1.Location = new Point(200, 230)
          Button1.TabIndex = 1
          Button1.Text = "&Close"
          Button1.Size = new Size(75, 23)
          AddHandler Button1.Click, New 
             System.EventHandler(AddressOf 
             Me.Button1_Click)

          Button2.Location = new Point(120, 230)
          Button2.TabIndex = 2
          Button2.Text = "&Execute"
          Button2.Size = new Size(75, 23)
          AddHandler Button2.Click, New 
             System.EventHandler(AddressOf 
             Me.Button2_Click)

          Label1.Location = new Point(8, 8)
          Label1.TabIndex = 0
          Label1.TabStop = False
          Label1.Text = ""
          Label1.Size = new Size(272, 232)

          Me.Text = "Client"

          Me.Controls.Add(Button2)
          Me.Controls.Add(Button1)
          Me.Controls.Add(Label1)

      End Sub

      Private Sub Button1_Click(ByVal sender As 
         System.Object, ByVal e As System.EventArgs) 
          Me.Close
      End Sub

      Private Sub Button2_Click(ByVal sender As 
         System.Object, ByVal e As System.EventArgs)
         
          ' Declare the local variables.
          Dim myCompCS As New CompCS.StringComponent
          Dim myCompVB As New CompVB.StringComponent
          Dim myCompVC As New CompVC.StringComponent

          Dim StringCount As Integer

          ' Clear the label. 
          Label1.Text = ""

          ' Display the results from the C# component.
          For StringCount = 0 To CInt(myCompCS.Count) - 1
            Label1.Text &= 
               MyCompCS.GetString(StringCount) & vbCrLf
          Next        
          Label1.Text &= vbCrLf

          ' Display the results from the Visual Basic component.
          For StringCount = 0 to CInt(MyCompVB.Count) - 1
              Label1.Text &= 
                 myCompVB.GetString(StringCount) & vbCrLf
          Next
          Label1.Text &= vbCrLf

          ' Display the results from the Visual C++ component.
          For StringCount = 0 To CInt(myCompVC.Count) - 1
              Label1.Text &= 
                 myCompVC.GetString(StringCount) & vbCrLf
          Next        
      End Sub
   End Class
End Module

在 .NET Framework SDK 中,Windows Form 程式庫位於 System.Windows.Forms 命名空間中。下列陳述式會將 System.Windows.Forms 命名空間合併到程式中。

Imports System.Windows.Forms

您可以藉由匯入命名空間來參考所包括的型別 (例如 Button),而不需要使用完整的型別名稱 (例如 System.Windows.Forms.Button)。

下面這一行程式碼就是說明繼承,這是 Common Language Runtime 中最強大的功能之一:

Inherits Form

您可以使用這個陳述式來指定用戶端類別繼承 Windows Form 程式庫中 Form 類別的所有功能。語言獨立是 Runtime 繼承 (Inheritance) 模型中很重要的一點,您不僅可以從 Runtime 繼承,也可以從任何以 .NET 語言撰寫的類別繼承。

接著,宣告即將用在表單中的物件型別,如下列這一行所示:

Private Button1 As System.Windows.Forms.Button

最後,就可以開始執行某些程式碼了。以下是 Client 表單的建構函式,它會建立基底類別的執行個體並呼叫 InitForm 方法:

Sub New()
   MyBase.New
   ' InitForm is required by the Windows Forms Designer.
   InitForm
End Sub

以下是程式本身的進入點,從建立 Client 表單的新執行個體開始:

Shared Sub Main()
   Application.Run(New Client)
End Sub

InitForm 方法會設定表單和其所有的控制項。例如,對 Button1 而言,InitForm 會從 Button 型別建立新的按鈕:

Me.Button1 = New Button

接著,InitForm 會移動它、設定它的標題 (或 Text 屬性),然後調整它的大小:

Button1.SetLocation(200, 248)
Button1.TabIndex = 1
Button1.Text = "&Close"
Button1.SetSize(75, 23)

接下來是巧妙的部分:將 Click (它只是 Button 型別的許多事件中的一種) 連接到範例自己的副程式:

AddHandler Button1.Click, New  
   System.EventHandler(AddressOf Me.Button1_Click)

最後,InitForm 會將按鈕加到表單的 Controls 集合中:

Me.Controls.Add(Button1)

當使用者按一下 Button1 時,下列程式碼會將事件處理常式反白顯示:

Private Sub Button1_Click(ByVal sender As System.Object, 
      ByVal e As System.EventArgs)
   Me.Close
End Sub

實際上,此處唯一改變的是呼叫表單的 Close 方法,如此便會結束應用程式。這個副程式會略過引數。

這個程式的重點在 Button2_Click 事件處理常式中,它使用了您在 Visual Basic 用戶端範例中見到的相同程式碼。但是,Windows Form 範例並不會寫入主控台,而是加入表單上標籤的 Text 屬性:

Label1.Text &= 
   myCompVC.GetString(StringCount) & vbCrLf

建置程序自然要複雜得多了。您不僅要指定已經建置的元件,也必須參考 Windows Form 需要的所有組件:

vbc.exe /t:winexe /debug+ /optionstrict+ 
   /reference:..\Bin\CompCS.dll 
   /reference:..\Bin\CompVB.dll 
   /reference:..\Bin\CompVC.dll 
   /reference:System.dll, 
   System.Windows.Forms.dll, 
   System.Data.DLL,system.drawing.dll 
   /out:..\bin\ClientWinForms.exe ClientWinForms.vb

執行應用程式會建立下面這個對話方塊。按一下 Execute 按鈕時,便會將字串寫入表單上的標籤中:

請參閱

使用 ASP.NET 的用戶端 | 開發教學課程摘要 | 附錄 A:瀏覽命名空間的工具