教學課:使用 Visual Basic 建立 WinForms 應用程式

在本教學課程中,您將建立具有 Windows Forms 使用者介面的 Visual Basic 應用程式。 Visual Studio 整合式開發環境 (IDE) 包含建立 Windows Forms 應用程式所需的所有工具。

在本教學課程中,您會了解如何:

  • 建立專案
  • 將按鈕新增至表單
  • 新增標籤和程式碼
  • 執行應用程式

必要條件

您需要 Visual Studio 才能完成本教學課程。 如需免費版本,請造訪 Visual Studio 下載頁面

您需要 Visual Studio 才能完成此教學課程。 如需免費版本,請造訪 Visual Studio 下載頁面

建立專案

建立 Visual Basic 應用程式專案。 在您新增任何項目之前,專案類型會隨附您需要的所有範本檔案。

  1. 開啟 Visual Studio。

  2. 在開始視窗中,選擇 [建立新專案]

    Screenshot shows the Visual Studio 2019 start window with Create a new project selected.

  3. 在 [建立新專案] 視窗中,選取 Visual Basic 的 [Windows Forms 應用程式 (.NET Framework)] 範本。

    您可以縮小搜尋範圍,快速取得您想要的範本。 例如,在搜尋方塊中輸入 Windows Forms 應用程式。 接下來,從 [語言] 清單中選取 [Visual Basic],然後從 [平台] 清單中選取 [Windows]

    Screenshot shows the Create a new project window with Windows Forms App (.NET Framework) selected.

    注意

    若未看到 [Windows Forms 應用程式 (.NET Framework)] 範本,可以從 [建立新專案] 視窗安裝。 在 [找不到你要尋找的項目嗎?] 訊息中,選擇 [安裝更多工具和功能] 連結。

    Screenshot shows the 'Install more tools and features' link from the 'Not finding what you're looking for' message.

    接著,在 Visual Studio 安裝程式中選擇 [.NET 桌面開發] 工作負載。

    Screenshot shows the .NET Core workload selected in the Visual Studio Installer.

    之後,在 Visual Studio 安裝程式中選取 [修改]。 系統可能會提示您儲存工作。

  4. 在 [設定您的新專案] 視窗中,輸入 HelloWorld 作為 [專案名稱]。 然後,選取 [建立]

    Screenshot shows the Configure your new project window with the name HelloWorld entered.

    Visual Studio 會隨即開啟您的新專案。

  1. 開啟 Visual Studio。

  2. 在開始視窗中,選擇 [建立新專案]

    Screenshot shows the Visual Studio 2022 start window with Create a new project selected.

  3. 在 [建立新專案] 視窗中,選取 Visual Basic 的 [Windows Forms 應用程式 (.NET Framework)] 範本。

    您可以縮小搜尋範圍,快速取得您想要的範本。 例如,在搜尋方塊中輸入 Windows Forms 應用程式。 接下來,從 [語言] 清單中選取 [Visual Basic],然後從 [平台] 清單中選取 [Windows]

    Screenshot shows the Create a new project window with Windows Forms App (.NET Framework) selected.

    注意

    若未看到 [Windows Forms 應用程式 (.NET Framework)] 範本,可以從 [建立新專案] 視窗安裝。 在 [找不到你要尋找的項目嗎?] 訊息中,選擇 [安裝更多工具和功能] 連結。

    Screenshot shows the 'Install more tools and features' link from the 'Not finding what you're looking for' message.

    接著,在 Visual Studio 安裝程式中選擇 [.NET 桌面開發] 工作負載。

    Screenshot shows the .NET Core workload selected in the Visual Studio Installer.

    之後,在 Visual Studio 安裝程式中選取 [修改]。 系統可能會提示您儲存工作。

  4. 在 [設定您的新專案] 視窗中,輸入 HelloWorld 作為 [專案名稱]。 然後,選取 [建立]

    Screenshot shows the Configure your new project window with the name HelloWorld entered.

    Visual Studio 會隨即開啟您的新專案。

將按鈕新增至表單

在您選取 Visual Basic 專案範本並命名檔案之後,Visual Studio 會為您開啟表單。 表單是 Windows 使用者介面。 您會將控制項新增至表單,以建立 "Hello World" 應用程式。

  1. 在 Visual Studio IDE 左側,選取 [工具箱] 索引標籤。若未看到,請從功能表列中選取 [檢視]> [工具箱],或按 Ctrl+Alt+X

    Screenshot shows the Toolbox tab that opens Toolbox window.

    如果您想要,請選取 [釘選] 圖示以固定 [工具箱] 視窗。

  2. 選取 [按鈕] 控制項,然後將它拖曳至表單。

    Screenshot shows the Button control added to the form.

  3. 在 [屬性] 視窗的 [外觀] 區段中,針對 [文字] 鍵入 Click this,然後按 Enter 鍵。

    Screenshot shows the Text property with the value Click this.

    如果未顯示 [屬性] 視窗,您可以從功能表列開啟。 選取 [檢視]>[屬性視窗],或按 F4

  4. 在 [屬性] 視窗的 [設計] 區段中,將名稱從 Button1 變更為 btnClickThis,然後按 Enter 鍵。

    Screenshot shows the Name property with a value of b t n click this.

    注意

    如果您在 [屬性] 視窗中將清單按字母順序排列,則 [Button1] 會改為出現在 (DataBindings) 區段中。

新增標籤和程式碼

既然您已新增可建立動作的按鈕控制項,請新增可將文字傳送至其中的標籤控制項。

  1. 選取 [工具箱] 中的 [標籤] 控制項,然後將其拖曳到表單上。 將它放在 [Click this] 按鈕之下。

  2. 在 [屬性] 視窗的 [設計] 區段或 [(DataBindings)] 區段中,將名稱 Label1 變更為 lblHelloWorld,然後按 Enter 鍵。

  3. 在 [Form1.vb [Design]] 視窗中,按兩下 [Click this] 按鈕以開啟 [Form1.vb] 視窗。

    另一個選項是在 [方案總管] 中展開 Form1.vb,然後選取 [Form1]

  4. 在 [Form1.vb] 視窗中 Private SubEnd Sub 行之間,輸入 lblHelloWorld.Text = "Hello World!",如下列螢幕擷取畫面所示:

    Screenshot shows the a class in the Form1.vs tab where you can add Visual Basic code.

執行應用程式

您的應用程式已準備好建置並執行。

  1. 選取 [開始] 以執行應用程式。

    Screenshot shows the Start button that runs your app.

    有好幾件事會發生。 在 Visual Studio IDE 中,開啟 [診斷工具] 視窗和 [輸出] 視窗都會開啟。 在 IDE 外部,會出現 Form1 對話方塊。 其中包含您的 [Click this] 按鈕,以及顯示 Label1 的文字。

  2. 選取 [Form1] 對話方塊中的 [Click this] 按鈕。

    Screenshot shows dialog box titled Form 1 that displays the text Hello World!

    Label1 文字會變更為 Hello World!

  3. 關閉 [Form1] 對話方塊以停止執行應用程式。

下一步

若要深入了解 Windows Forms,請繼續進行下列教學課程:

另請參閱