使用文字編輯器建立簡單的 VB.NET WinForms 應用程式

  1. 開啟文字編輯器(如記事本),然後鍵入以下程式碼:

     Imports System.ComponentModel
     Imports System.Drawing
     Imports System.Windows.Forms
    
     Namespace SampleApp
         Public Class MainForm : Inherits Form
             Private btnHello As Button
    
             ' The form's constructor: this initializes the form and its controls.
             Public Sub New()
                 ' Set the form's caption, which will appear in the title bar.
                 Me.Text = "MainForm"
    
                 ' Create a button control and set its properties.
                 btnHello = New Button()
                 btnHello.Location = New Point(89, 12)
                 btnHello.Name = "btnHello"
                 btnHello.Size = New Size(105, 30)
                 btnHello.Text = "Say Hello"
    
                 ' Wire up an event handler to the button's "Click" event
                 ' (see the code in the btnHello_Click function below).
                 AddHandler btnHello.Click, New EventHandler(AddressOf btnHello_Click)
    
                 ' Add the button to the form's control collection,
                 ' so that it will appear on the form.
                 Me.Controls.Add(btnHello)
             End Sub
    
             ' When the button is clicked, display a message.
             Private Sub btnHello_Click(sender As Object, e As EventArgs)
                 MessageBox.Show("Hello, World!")
             End Sub
    
             ' This is the main entry point for the application.
             ' All VB.NET applications have one and only one of these methods.
             <STAThread> _
             Public Shared Sub Main()
                 Application.EnableVisualStyles()
                 Application.Run(New MainForm())
             End Sub
         End Class
     End Namespace
    
  2. 將檔案儲存到你具有讀/寫訪問許可權的路徑。在包含它的類之後命名檔案是常規的 -​​ 例如,X:\MainForm.vb

  3. 從命令列執行 VB.NET 編譯器,將路徑作為引數傳遞給程式碼檔案:

     %WINDIR%\Microsoft.NET\Framework64\v4.0.30319\vbc.exe /target:winexe "X:\MainForm.vb"
    

    注意: 要將 VB.NET 編譯器的一個版本用於其他 .NET 框架版本,請檢視路徑%WINDIR%\Microsoft.NET 並相應地修改上面的示例。有關編譯 VB.NET 應用程式的更多資訊,請參閱 Hello World

  4. 編譯完成後,將在與程式碼檔案相同的目錄中建立名為 MainForm.exe 的應用程式。你可以從命令列執行此應用程式,也可以在資源管理器中雙擊它。