教學課程:建立簡單的 Visual Basic (VB) 主控台應用程式

本文說明如何使用Visual Studio建立簡單的Visual Basic應用程式,也就是 控制台應用程式。 在此應用程式中,您會要求使用者提供其名稱,然後加上目前時間顯示。 您也會探索 Visual Studio 集成開發環境 (IDE)一些功能,包括 Git 中的原始檔控制。 Visual Basic 是一種類型安全的程式設計語言,設計成容易了解。 主控台應用程式接受輸入,並會在命令列視窗 (也稱為主控台) 中顯示輸出。

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

  • 建立 Visual Studio 專案
  • 執行預設應用程式
  • 新增程式碼以要求使用者輸入
  • 額外點數:新增兩個數字
  • 新增 Git 原始程式碼控制
  • 清除資源

必要條件

如果您尚未安裝 Visual Studio,請前往 Visual Studio 下載頁面免費進行安裝。

建立專案

首先,您會建立 Visual Basic 應用程式專案。 默認專案範本包含可執行應用程式所需的所有檔案。

注意

本教學課程中的某些螢幕擷取畫面使用深色佈景主題。 如果您未使用深色佈景主題,但想要使用,請參閱將 Visual Studio IDE 和編輯器個人化頁面以了解做法。

  1. 開啟 Visual Studio 2019。

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

    Screenshot showing the Visual Studio start window with 'Create a new project' selected.

  3. 在 [建立新專案] 視窗中,從 [語言] 清單中選擇 [Visual Basic]。 接著,從平台清單中選擇 [Windows],然後從專案類型清單中選取 [主控台]

    套用語言、平臺和專案類型篩選之後,選擇 [控制台應用程式 ] 範本,然後選擇 [ 下一步]。

    Screenshot that shows how to choose the Visual Basic, Windows, and Console filter options and select the Console Application project template.

    注意

    如果您未看到 [主控台應用程式] 範本,您可以從 [建立新專案] 視窗中安裝。 在 [找不到你要尋找的項目嗎?] 訊息中,選擇 [安裝更多工具和功能] 連結。

    Screenshot showing the 'Install more tools and features' link from the 'Not finding what you're looking for' message in the 'Create new project' window.

    接下來,在 Visual Studio 安裝程式中選擇 .NET Core 跨平台開發工作負載。

    Screenshot showing the .NET Core cross-platform development workload in the Visual Studio Installer.

    接著,選擇Visual Studio 安裝程式中的 [修改] 按鈕。 系統可能會提示您儲存工作。 接下來,選擇 [繼續] 以安裝工作負載。 然後,返回這個建立專案程序中的第 2 步。

  4. 在 [設定您的新專案] 視窗的 [專案名稱] 方塊中輸入 WhatIsYourName。 接著,選擇 [下一步]

    Screenshot showing the 'Configure your new project' window in Visual Studio with the Project name field set to 'WhatIsYourName'.

  5. 在 [其他資訊] 視窗中,應該已針對目標架構選取 [.NET 5.0 (目前)]。 如果沒有,請選取 [.NET 5.0 (目前)]。 接著,選擇 [建立]

    Screenshot showing the Additional information window in Visual Studio with .NET 5.0 (Current) selected as the target framework for the new project.

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

  1. 開啟 Visual Studio。

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

    Screenshot showing the Visual Studio start window with 'Create a new project' selected.

  3. 在 [建立新專案] 視窗中,從 [語言] 清單中選擇 [Visual Basic]。 接著,從平台清單中選擇 [Windows],然後從專案類型清單中選取 [主控台]

    套用語言、平臺和專案類型篩選之後,選擇 [控制台應用程式 ] 範本,然後選擇 [ 下一步]。

    Screenshot that shows how to choose the Visual Basic, Windows, and Console filter options and select the Console Application project template.

    注意

    如果您未看到 [主控台應用程式] 範本,您可以從 [建立新專案] 視窗中安裝。 在 [找不到你要尋找的項目嗎?] 訊息中,選擇 [安裝更多工具和功能] 連結。

    Screenshot showing the 'Install more tools and features' link from the 'Not finding what you're looking for' message in the 'Create new project' window.

    然後,在 Visual Studio 安裝程式中選擇 .NET 桌面開發工作負載。

    Screenshot showing the .NET desktop development workload in the Visual Studio Installer.

    接著,選擇Visual Studio 安裝程式中的 [修改] 按鈕。 系統可能會提示您儲存工作。 接下來,選擇 [繼續] 以安裝工作負載。 然後,返回這個建立專案程序中的第 2 步。

  4. 在 [設定您的新專案] 視窗的 [專案名稱] 方塊中輸入 WhatIsYourName。 接著,選擇 [下一步]

    Screenshot showing the 'Configure your new project' window in Visual Studio with the Project name field set to 'WhatIsYourName'.

  5. 在 [ 其他資訊] 視窗中, 應該已為您的目標架構選取 .NET 8.0 。 如果沒有,請選取 [.NET 8.0]。 接著,選擇 [建立]

    Screenshot showing the Additional information window in Visual Studio with .NET 8.0 selected as the target framework for the new project.

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

執行應用程式

在您選取 Visual Basic 專案範本並命名專案之後,Visual Studio 會建立 Program.vb 檔案。 預設程式碼會呼叫 WriteLine 方法,在主控台視窗中顯示常值字串 "Hello World!"。

有兩種方法可以執行此程式碼,在 Visual Studio 內的偵錯模式中,以及從您的電腦以一般獨立應用程式形式執行。

以偵錯模式執行應用程

Screenshot showing the default 'Hello World!' code.

  1. 選取 [WhatIsYourName] 按鈕或按下 F5,在偵錯模式中執行預設程式碼。

    Screenshot showing the 'What Is Your Name' button highlighted in the Visual Studio toolbar.

  2. 當應用程式在 Microsoft Visual Studio 偵錯主控台中執行時,"Hello World!" 會顯示。 按任意鍵以關閉偵錯主控台視窗並結束應用程式:

    Screenshot showing 'Hello World!' and the 'Press any key to close this window' messages.

Screenshot showing the default 'Hello World!' code.

  1. 選取 [WhatIsYourName] 按鈕或按下 F5,在偵錯模式中執行預設程式碼。

    Screenshot showing the 'What Is Your Name' button highlighted in the Visual Studio toolbar.

  2. 當應用程式在 Microsoft Visual Studio 偵錯主控台中執行時,"Hello World!" 會顯示。 按任意鍵以關閉偵錯主控台視窗並結束應用程式:

    Screenshot showing 'Hello World!' and the 'Press any key to close this window' messages when the app runs in the Debug Console.

以獨立形式執行應用程式

若要查看 Visual Studio 外部的輸出,請在系統主控台視窗中建置並執行可執行檔 (.exe 檔案)。

  1. 在 [建置] 功能表中,選擇 [建置解決方案]

  2. 在 [方案總管] 中,以滑鼠右鍵按一下 [WhatIsYourName],然後選擇 [在檔案總管中開啟檔案]

  3. 在 [檔案總管] 中,瀏覽至 bin\Debug\net5.0 目錄並且執行 WhatIsYourName.exe

  4. Main 程序會在其單一陳述式執行之後終止,主控台視窗會立即關閉。 若要讓主控台直到使用者按下按鍵為止保持可見,請參閱下一節。

  1. 在 [建置] 功能表中,選擇 [建置解決方案]

  2. 在 [方案總管] 中,以滑鼠右鍵按一下 [WhatIsYourName],然後選擇 [在檔案總管中開啟檔案]

  3. 檔案總管 中,流覽至 bin\Debug\net8.0 目錄,然後執行WhatIsYourName.exe

  4. Main 程序會在其單一陳述式執行之後終止,主控台視窗會立即關閉。 若要讓主控台直到使用者按下按鍵為止保持可見,請參閱下一節。

新增程式碼以要求使用者輸入

接下來,您會新增 Visual Basic 程式代碼,提示您輸入名稱,然後連同目前的日期和時間一起顯示它。 此外,您會新增可暫停控制台視窗的程式代碼,直到使用者按下按鍵為止。

  1. Sub Main(args As String()) 行後面與 End Sub 行前面輸入下列 Visual Basic 程式碼,取代 WriteLine 行:

    Console.Write("Please enter your name: ")
    Dim name = Console.ReadLine()
    Dim currentDate = DateTime.Now
    Console.WriteLine($"Hello, {name}, on {currentDate:d} at {currentDate:t}")
    Console.Write("Press any key to continue...")
    Console.ReadKey(True)
    
    • WriteWriteLine 會將字串寫入到主控台。
    • ReadLine 會從主控台讀取輸入,在此案例中為字串。
    • DateTime 代表日期時間,Now 會傳回目前的時間。
    • ReadKey() 會暫停應用程式,並等候按鍵動作。

    Screenshot showing the code for the 'Program.vb' file in the 'WhatIsYourName' project loaded in the Visual Basic code editor.

  2. 選取 [WhatIsYourName] 按鈕或按下 F5,在偵錯模式中建置並執行您的第一個應用程式。

  3. 偵錯主控台視窗開啟時,請輸入您的名稱。 主控台視窗應該類似下列螢幕擷取畫面:

    Screenshot showing the debug console window with 'Please enter your name', the date and time, and 'Press any key to continue' messages.

  4. 按任意鍵以結束應用程式,然後按任意鍵以關閉偵錯主控台視窗。

  1. Sub Main(args As String()) 行後面與 End Sub 行前面輸入下列 Visual Basic 程式碼,取代 WriteLine 行:

    Console.Write("Please enter your name: ")
    Dim name = Console.ReadLine()
    Dim currentDate = DateTime.Now
    Console.WriteLine($"Hello, {name}, on {currentDate:d} at {currentDate:t}")
    Console.Write("Press any key to continue...")
    Console.ReadKey(True)
    
    • WriteWriteLine 會將字串寫入到主控台。
    • ReadLine 會從主控台讀取輸入,在此案例中為字串。
    • DateTime 代表日期時間,Now 會傳回目前的時間。
    • ReadKey() 會暫停應用程式,並等候按鍵動作。

    Screenshot showing the code for the 'Program.vb' file in the 'WhatIsYourName' project loaded in the Visual Basic code editor.

  2. 選取 [WhatIsYourName] 按鈕或按下 F5,在偵錯模式中建置並執行您的應用程式。

  3. 偵錯主控台視窗開啟時,請輸入您的名稱。 主控台視窗應該類似下列螢幕擷取畫面:

    Screenshot showing the debug console window with 'Please enter your name', the date and time, and 'Press any key to continue' messages.

  4. 按任意鍵以結束應用程式,然後按任意鍵以關閉偵錯主控台視窗。

既然您的新程式碼已在應用程式中,請在系統主控台視窗中建置並執行可執行檔 (.exe 檔案),如先前在以獨立形式執行應用程式中所述。 現在當您按下按鍵時,應用程式會結束,這會關閉主控台視窗。

額外點數:新增兩個數字

此範例示範如何以數字而非字串讀取,並執行一些算術。 請嘗試從以下位置變更您的程式碼:

Module Program
    Sub Main(args As String())
        Console.Write("Please enter your name: ")
        Dim name = Console.ReadLine()
        Dim currentDate = DateTime.Now
        Console.WriteLine($"Hello, {name}, on {currentDate:d} at {currentDate:t}")
        Console.Write("Press any key to continue...")
        Console.ReadKey(True)
    End Sub
End Module

變更為:

Module Program
    Public num1 As Integer
    Public num2 As Integer
    Public answer As Integer
    Sub Main(args As String())
        Console.Write("Type a number and press Enter")
        num1 = Console.ReadLine()
        Console.Write("Type another number to add to it and press Enter")
        num2 = Console.ReadLine()
        answer = num1 + num2
        Console.WriteLine("The answer is " & answer)
        Console.Write("Press any key to continue...")
        Console.ReadKey(True)
    End Sub
End Module

然後執行更新的應用程式,如上一節所述, 執行應用程式

新增 Git 原始程式碼控制

既然您已建立應用程式,您可能就想要將其新增至 Git 存放庫。 Visual Studio 會透過您可以直接從 IDE 使用的 Git 工具,讓該流程變得容易。

提示

Git 是最廣泛使用的新式版本控制系統,因此無論您是專業開發人員,還是您正在學習如何撰寫程式碼,Git 都非常有用。 如果您不熟悉 Git,https://git-scm.com/ 網站是很好的起點。 在那裡,您可以在找到速查表、熱門的線上書籍和 Git 基本概念影片。

若要將程式碼與 Git 建立關聯,請先建立程式碼所在的新 Git 存放庫:

  1. 在 Visual Studio 右下角的狀態列中,選取 [新增至原始程式碼控制],然後選取 [Git]

    Screenshot of the Git source control buttons below the Solution Explorer pane, with the Add to Source Control button highlighted.

  2. 在 [建立 Git 存放庫] 對話方塊中,登入 GitHub。

    Screenshot of the Create a Git Repository dialog window where you can sign in to GitHub.

    存放庫名稱會根據您的資料夾位置自動填入。 您的新存放庫預設是私人的,這表示您是唯一可以存取此存放庫的人員。

    提示

    無論您的存放庫是公用還是私人,最好有程式碼的遠端備份安全地儲存在 GitHub 上。 即使您未與小組合作,遠端存放庫仍可讓您從任何電腦使用您的程式碼。

  3. 選取 [建立並推送]

    在建立您的存放庫之後,您會在狀態列中看到狀態詳細資料。

    Screenshot of the repo status bar that's below the Solution Explorer pane in Visual Studio.

    第一個具有箭號的圖示會顯示您的最新分支中有多少個傳出/傳入認可。 您可以使用此圖示來提取任何傳入認可或推送任何傳出認可。 您也可以選擇先檢視這些認可。 若要這樣做,請選取圖示,然後選取 [檢視傳出/傳入]

    具有鉛筆的第二個圖示會顯示程式碼未認可的變更數目。 您可以選取此圖示,在 [Git 變更] 視窗中檢視這些變更。

若要深入了解如何搭配您的應用程式使用 Git,請參閱 Visual Studio 版本控制文件

清除資源

如果您不打算繼續使用此應用程式,請刪除專案。

  1. 在 [方案總管] 中,以滑鼠右鍵按一下 [WhatIsYourName],以開啟專案的捷徑功能表。 然後選取 [在檔案總管中開啟資料夾]

  2. 關閉 Visual Studio。

  3. 在 [檔案總管] 對話方塊中,上移兩個資料夾層級。

  4. 以滑鼠右鍵按一下 [WhatIsYourName] 資料夾,然後選取 [刪除]

下一步

恭喜您完成此教學課程! 若要深入了解,請參閱下列教學課程。

另請參閱