winforms 使用文本编辑器创建一个简单的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.
            ' AllVB.NETapplications 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
  1. 将文件保存到您具有读/写访问权限的路径。通常,以文件所包含的类(例如)命名X:\MainForm.vb。

  1. VB.NET从命令行运行编译器,并将路径作为参数传递给代码文件:

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

    注意:要将VB.NET编译器版本用于其他.NET Framework版本,请查看路径%WINDIR%\Microsoft.NET并相应地修改上面的示例。有关编译VB.NET应用程序的更多信息,请参见Hello World。

  1. 编译完成后,MainForm.exe将在与代码文件相同的目录中创建一个名为的应用程序。您可以从命令行运行该应用程序,也可以在资源管理器中双击该应用程序。