■Visual Basicによるコード例(フォームを使ったアプリケーション)

(赤字はコードに対するコメント)

 1: Imports System
 2: Imports System.Collections
 3: Imports System.Core
 4: Imports System.ComponentModel
 5: Imports System.Drawing
 6: Imports System.Data
 7: Imports System.WinForms
 8:
 9: Namespace VB_WinApp
10:     Public Class Form1
11:         Inherits System.WinForms.Form ←.NET Frameworkのクラスから継承
12:         'Required by the Win Form Designer
13:         Private components As System.ComponentModel.Container
14:         Private LinkLabel1 As System.WinForms.LinkLabel
15:
16:         Public Sub New()
17:             MyBase.New()
18:             'This call is required by the Win Form Designer.
19:             InitializeComponent()
20:             'TODO: Add any initialization after the InitForm call
21:         End Sub
22:
23:         'Form overrides dispose to clean up the component list.
24:         Overrides Public Sub Dispose() ←VBでもオーバーライドが可能
25:             MyBase.Dispose()
26:             components.Dispose()
27:         End Sub
28:
29:         'The main entry point for the application
30:         Shared Sub Main() ←Main()からコードの実行が始まる
31:             System.WinForms.Application.Run(New Form1) ←クラスライブラリのメソッド呼び出し
32:         End Sub
33:
34:         'NOTE: The following procedure is required by the Win Form Designer
35:         'It can be modified using the Win Form Designer.
36:         'Do not modify it using the code editor.
37:         Private Sub InitializeComponent() ←コントロールはすべてコード内で記述される
38:             Me.components = New System.ComponentModel.Container
39:             Me.LinkLabel1 = New System.WinForms.LinkLabel
40:
41:             LinkLabel1.Location = New System.Drawing.Point(16, 24)
42:             LinkLabel1.Text = "LinkLabel1"
43:             LinkLabel1.Size = New System.Drawing.Size(160, 16)
44:             LinkLabel1.TabIndex = 0
45:
46:             Me.AutoScaleBaseSize = New System.Drawing.Size(5, 12)
47:             Me.Text = "Form1"
48:             '@design Me.TrayLargeIcon = True
49:             '@design Me.TrayHeight = 0
50:             Me.AddOnClick(New System.EventHandler(AddressOf Me.Form1_Click)) ←ボタンのイベント・ハンドラを設定
51:
52:             Me.Controls.Add(LinkLabel1)
53:
54:         End Sub
55:
56:         Protected Sub Form1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) ←ボタンが押されたら呼び出されるサブルーチン
57:         End Sub
58:     End Class
59: End Namespace