VB.Net Tutorial/GUI/Form Action Event

Материал из VB Эксперт
Перейти к: навигация, поиск

Add Key event to Form window

Imports System.Windows.Forms
public class FormEventKey
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class

Public Class Form1
    Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
    Public Sub New()
        MyBase.New()
        "This call is required by the Windows Form Designer.
        InitializeComponent()
        "Add any initialization after the InitializeComponent() call
    End Sub
    "Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub
    "Required by the Windows Form Designer
    Private components As System.ruponentModel.IContainer
    "NOTE: The following procedure is required by the Windows Form Designer
    "It can be modified using the Windows Form Designer.  
    "Do not modify it using the code editor.
    Friend WithEvents keyDataLabel As System.Windows.Forms.Label
    Friend WithEvents label1 As System.Windows.Forms.Label
    Friend WithEvents label2 As System.Windows.Forms.Label
    Friend WithEvents keyCodeLabel As System.Windows.Forms.Label
    Friend WithEvents modifiersLabel As System.Windows.Forms.Label
    Friend WithEvents label4 As System.Windows.Forms.Label
    Friend WithEvents keyValueLabel As System.Windows.Forms.Label
    Friend WithEvents label5 As System.Windows.Forms.Label
    Friend WithEvents label3 As System.Windows.Forms.Label
    Friend WithEvents keyCharLabel As System.Windows.Forms.Label
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.keyDataLabel = New System.Windows.Forms.Label()
        Me.label1 = New System.Windows.Forms.Label()
        Me.label2 = New System.Windows.Forms.Label()
        Me.keyCodeLabel = New System.Windows.Forms.Label()
        Me.modifiersLabel = New System.Windows.Forms.Label()
        Me.label4 = New System.Windows.Forms.Label()
        Me.keyValueLabel = New System.Windows.Forms.Label()
        Me.label5 = New System.Windows.Forms.Label()
        Me.label3 = New System.Windows.Forms.Label()
        Me.keyCharLabel = New System.Windows.Forms.Label()
        Me.SuspendLayout()
        "
        "keyDataLabel
        "
        Me.keyDataLabel.Anchor = ((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right)
        Me.keyDataLabel.Location = New System.Drawing.Point(120, 8)
        Me.keyDataLabel.Name = "keyDataLabel"
        Me.keyDataLabel.Size = New System.Drawing.Size(228, 23)
        Me.keyDataLabel.TabIndex = 9
        "
        "label1
        "
        Me.label1.Location = New System.Drawing.Point(8, 8)
        Me.label1.Name = "label1"
        Me.label1.TabIndex = 5
        Me.label1.Text = "KeyData"
        "
        "label2
        "
        Me.label2.Location = New System.Drawing.Point(8, 40)
        Me.label2.Name = "label2"
        Me.label2.TabIndex = 6
        Me.label2.Text = "KeyCode"
        "
        "keyCodeLabel
        "
        Me.keyCodeLabel.Anchor = ((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right)
        Me.keyCodeLabel.Location = New System.Drawing.Point(120, 40)
        Me.keyCodeLabel.Name = "keyCodeLabel"
        Me.keyCodeLabel.Size = New System.Drawing.Size(228, 23)
        Me.keyCodeLabel.TabIndex = 10
        "
        "modifiersLabel
        "
        Me.modifiersLabel.Anchor = ((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right)
        Me.modifiersLabel.Location = New System.Drawing.Point(120, 72)
        Me.modifiersLabel.Name = "modifiersLabel"
        Me.modifiersLabel.Size = New System.Drawing.Size(228, 23)
        Me.modifiersLabel.TabIndex = 11
        "
        "label4
        "
        Me.label4.Location = New System.Drawing.Point(8, 72)
        Me.label4.Name = "label4"
        Me.label4.TabIndex = 4
        Me.label4.Text = "Modifiers"
        "
        "keyValueLabel
        "
        Me.keyValueLabel.Anchor = ((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right)
        Me.keyValueLabel.Location = New System.Drawing.Point(120, 104)
        Me.keyValueLabel.Name = "keyValueLabel"
        Me.keyValueLabel.Size = New System.Drawing.Size(228, 23)
        Me.keyValueLabel.TabIndex = 8
        "
        "label5
        "
        Me.label5.Location = New System.Drawing.Point(8, 104)
        Me.label5.Name = "label5"
        Me.label5.TabIndex = 3
        Me.label5.Text = "KeyValue"
        "
        "label3
        "
        Me.label3.Location = New System.Drawing.Point(8, 136)
        Me.label3.Name = "label3"
        Me.label3.TabIndex = 2
        Me.label3.Text = "KeyChar"
        "
        "keyCharLabel
        "
        Me.keyCharLabel.Anchor = ((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right)
        Me.keyCharLabel.Location = New System.Drawing.Point(120, 136)
        Me.keyCharLabel.Name = "keyCharLabel"
        Me.keyCharLabel.Size = New System.Drawing.Size(228, 23)
        Me.keyCharLabel.TabIndex = 7
        "
        "Form1
        "
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(328, 174)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.keyDataLabel, Me.label1, Me.label2, Me.keyCodeLabel, Me.modifiersLabel, Me.label4, Me.keyValueLabel, Me.label5, Me.label3, Me.keyCharLabel})
        Me.Name = "Form1"
        Me.Text = "KeyTest"
        Me.ResumeLayout(False)
    End Sub
#End Region
    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        keyDataLabel.Text = e.KeyData.ToString()
        keyCodeLabel.Text = e.KeyCode.ToString()
        modifiersLabel.Text = e.Modifiers.ToString()
        keyValueLabel.Text = e.KeyValue.ToString()
    End Sub
    Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
        keyCharLabel.Text = e.KeyChar.ToString()
    End Sub
End Class

Close a Form

Imports System.Windows.Forms
public class FormCloseAction
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class

Public Class Form1
    Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
    Public Sub New()
        MyBase.New()
        "This call is required by the Windows Form Designer.
        InitializeComponent()
        "Add any initialization after the InitializeComponent() call
    End Sub
    "Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If Disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(Disposing)
    End Sub
    Private WithEvents btnClose As System.Windows.Forms.Button
    "Required by the Windows Form Designer
    Private components As System.ruponentModel.Container
    "NOTE: The following procedure is required by the Windows Form Designer
    "It can be modified using the Windows Form Designer.  
    "Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.btnClose = New System.Windows.Forms.Button()
        Me.SuspendLayout()
        "
        "btnClose
        "
        Me.btnClose.Location = New System.Drawing.Point(108, 102)
        Me.btnClose.Name = "btnClose"
        Me.btnClose.Size = New System.Drawing.Size(56, 32)
        Me.btnClose.TabIndex = 0
        Me.btnClose.Text = "Close"
        "
        "Form1
        "
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(272, 237)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnClose})
        Me.Name = "Form1"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "Form1"
        Me.ResumeLayout(False)
    End Sub
#End Region
    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Dim drQuit As DialogResult
        drQuit = MessageBox.Show("Do you really want to quit?", _
            "Exit Confirmation", _
            MessageBoxButtons.YesNo, _
            MessageBoxIcon.Warning, _
            MessageBoxDefaultButton.Button2)
        If drQuit = DialogResult.Yes Then Me.Close()
    End Sub
End Class

Convert Mouse event to form caption drag and drop event

Imports System.Windows.Forms
public class ConvertMouseEventToFormDragEvent
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class

Public Class Form1
    Const HT_CAPTION As Integer = &H2
    Const WM_NCLBUTTONDOWN As Integer = &HA1
    Private Sub DragBar_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DragBar.MouseDown
        If (e.Button = Windows.Forms.MouseButtons.Left) Then
            DragBar.Capture = False
            Me.WndProc(Message.Create(Me.Handle, WM_NCLBUTTONDOWN, _
                CType(HT_CAPTION, IntPtr), IntPtr.Zero))
        End If
    End Sub
    Private Sub ActClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ActClose.Click
        Me.Close()
    End Sub
End Class

<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form
    "Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        End If
        MyBase.Dispose(disposing)
    End Sub
    "Required by the Windows Form Designer
    Private components As System.ruponentModel.IContainer
    "NOTE: The following procedure is required by the Windows Form Designer
    "It can be modified using the Windows Form Designer.  
    "Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.ActClose = New System.Windows.Forms.Button
        Me.DragBar = New System.Windows.Forms.PictureBox
        CType(Me.DragBar, System.ruponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        "
        "ActClose
        "
        Me.ActClose.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.ActClose.Location = New System.Drawing.Point(208, 80)
        Me.ActClose.Name = "ActClose"
        Me.ActClose.Size = New System.Drawing.Size(75, 23)
        Me.ActClose.TabIndex = 0
        Me.ActClose.Text = "Close"
        Me.ActClose.UseVisualStyleBackColor = True
        "
        "DragBar
        "
        Me.DragBar.BackColor = System.Drawing.SystemColors.ActiveCaption
        Me.DragBar.Location = New System.Drawing.Point(8, 8)
        Me.DragBar.Name = "DragBar"
        Me.DragBar.Size = New System.Drawing.Size(280, 24)
        Me.DragBar.TabIndex = 1
        Me.DragBar.TabStop = False
        "
        "Form1
        "
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.CancelButton = Me.ActClose
        Me.ClientSize = New System.Drawing.Size(292, 114)
        Me.Controls.Add(Me.DragBar)
        Me.Controls.Add(Me.ActClose)
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None
        Me.Name = "Form1"
        Me.Text = "Form1"
        CType(Me.DragBar, System.ruponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)
    End Sub
    Friend WithEvents ActClose As System.Windows.Forms.Button
    Friend WithEvents DragBar As System.Windows.Forms.PictureBox
End Class

Form Events Illustration

Imports System.Diagnostics
Imports System.Windows.Forms
public class FormEvents
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class

Public Class Form1
    Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
    Public Sub New()
        MyBase.New()
        "This call is required by the Windows Form Designer.
        InitializeComponent()
        "Add any initialization after the InitializeComponent() call
        Console.WriteLine("New Event Fired")
    End Sub
    "Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        Console.WriteLine("Dispose Method Executed")
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub
    Friend WithEvents btnLoop As System.Windows.Forms.Button
    Friend WithEvents btnRemember As System.Windows.Forms.Button
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents Button2 As System.Windows.Forms.Button
    Friend WithEvents Button3 As System.Windows.Forms.Button
    "Required by the Windows Form Designer
    Private components As System.ruponentModel.IContainer
    "NOTE: The following procedure is required by the Windows Form Designer
    "It can be modified using the Windows Form Designer.  
    "Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.btnLoop = New System.Windows.Forms.Button()
        Me.btnRemember = New System.Windows.Forms.Button()
        Me.Button1 = New System.Windows.Forms.Button()
        Me.Button2 = New System.Windows.Forms.Button()
        Me.Button3 = New System.Windows.Forms.Button()
        Me.SuspendLayout()
        "
        "btnLoop
        "
        Me.btnLoop.Location = New System.Drawing.Point(24, 56)
        Me.btnLoop.Name = "btnLoop"
        Me.btnLoop.TabIndex = 0
        Me.btnLoop.Text = "Loop"
        "
        "btnRemember
        "
        Me.btnRemember.Location = New System.Drawing.Point(136, 56)
        Me.btnRemember.Name = "btnRemember"
        Me.btnRemember.Size = New System.Drawing.Size(112, 23)
        Me.btnRemember.TabIndex = 1
        Me.btnRemember.Text = "Remember Position"
        "
        "Button1
        "
        Me.Button1.Location = New System.Drawing.Point(24, 16)
        Me.Button1.Name = "Button1"
        Me.Button1.TabIndex = 2
        Me.Button1.Text = "Button1"
        "
        "Button2
        "
        Me.Button2.Location = New System.Drawing.Point(128, 16)
        Me.Button2.Name = "Button2"
        Me.Button2.TabIndex = 3
        Me.Button2.Text = "Button2"
        "
        "Button3
        "
        Me.Button3.Location = New System.Drawing.Point(224, 16)
        Me.Button3.Name = "Button3"
        Me.Button3.Size = New System.Drawing.Size(72, 24)
        Me.Button3.TabIndex = 4
        Me.Button3.Text = "Button3"
        "
        "Form1
        "
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(320, 134)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button3, Me.Button2, Me.Button1, Me.btnRemember, Me.btnLoop})
        Me.Name = "Form1"
        Me.Text = "Event-O-Rama"
        Me.ResumeLayout(False)
    End Sub
#End Region
    Dim formsize As System.Drawing.Size
    Dim formlocation As System.Drawing.Point

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Console.WriteLine("Load Event Fired")
        formsize = Me.Size
        formlocation = Me.Location
    End Sub
    Private Sub Form1_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Activated
        Console.WriteLine("Activated Event Fired")
    End Sub
    Private Sub Form1_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
        Console.WriteLine("Closed Event Fired")
    End Sub
    Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ruponentModel.CancelEventArgs) Handles MyBase.Closing
        Console.WriteLine("Closing Event Fired")
        Dim answer As MsgBoxResult
        answer = MsgBox("Do you want to close this window?", MsgBoxStyle.YesNo, "Demonstration of the Closing event")
        If answer = MsgBoxResult.No Then
            e.Cancel = True
        End If
    End Sub
    Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Click
        Console.WriteLine("Click Event Fired")
    End Sub
    Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
        Console.WriteLine("MouseMove Event Fired")
    End Sub
    Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize
        Console.WriteLine("Resize Event Fired " & Me.Size.Width.ToString())
    End Sub
    Private Sub Form1_Deactivate(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Deactivate
        Console.WriteLine("Deactivate Event Fired")
    End Sub
    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        Console.WriteLine("Paint Event Fired")
    End Sub
    Private Sub Form1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Leave
        Console.WriteLine("Leave Event Fired")
    End Sub
    Private Sub Form1_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Enter
        Console.WriteLine("Enter Event Fired")
    End Sub

    Private Sub btnLoop_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLoop.Enter
        Console.WriteLine("btnLoop Enter Event Fired")
        "btnLoop_Leave(sender, e)
    End Sub
    Private Sub btnLoop_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLoop.Leave
        Console.WriteLine("btnLoop Leave Event Fired")
        "btnLoop_Enter(sender, e)
    End Sub
    Private Sub btnRemember_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRemember.Click
        Me.Size = formsize
        Me.Location = formlocation
    End Sub
    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
        Handles Button1.Click, Button2.Click, Button3.Click
      MsgBox(sender.Text & " was clicked!")
    End Sub
End Class

Form Layout event

Imports System.Windows.Forms
public class FormLayoutEvent
   public Shared Sub Main
        Application.Run(New GridForm)
   End Sub
End class
Public Class GridForm
    Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
    Public Sub New()
        MyBase.New()
        "This call is required by the Windows Form Designer.
        InitializeComponent()
        "Add any initialization after the InitializeComponent() call
    End Sub
    "Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub
    "Required by the Windows Form Designer
    Private components As System.ruponentModel.IContainer
    "NOTE: The following procedure is required by the Windows Form Designer
    "It can be modified using the Windows Form Designer.  
    "Do not modify it using the code editor.
    Friend WithEvents button1 As System.Windows.Forms.Button
    Friend WithEvents button2 As System.Windows.Forms.Button
    Friend WithEvents button3 As System.Windows.Forms.Button
    Friend WithEvents button4 As System.Windows.Forms.Button
    Friend WithEvents button5 As System.Windows.Forms.Button
    Friend WithEvents button6 As System.Windows.Forms.Button
    Friend WithEvents button7 As System.Windows.Forms.Button
    Friend WithEvents button8 As System.Windows.Forms.Button
    Friend WithEvents button9 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.button1 = New System.Windows.Forms.Button()
        Me.button2 = New System.Windows.Forms.Button()
        Me.button3 = New System.Windows.Forms.Button()
        Me.button4 = New System.Windows.Forms.Button()
        Me.button5 = New System.Windows.Forms.Button()
        Me.button6 = New System.Windows.Forms.Button()
        Me.button7 = New System.Windows.Forms.Button()
        Me.button8 = New System.Windows.Forms.Button()
        Me.button9 = New System.Windows.Forms.Button()
        Me.SuspendLayout()
        "
        "button1
        "
        Me.button1.Location = New System.Drawing.Point(8, 8)
        Me.button1.Name = "button1"
        Me.button1.Size = New System.Drawing.Size(75, 75)
        Me.button1.TabIndex = 7
        Me.button1.Text = "1"
        "
        "button2
        "
        Me.button2.Location = New System.Drawing.Point(88, 8)
        Me.button2.Name = "button2"
        Me.button2.Size = New System.Drawing.Size(75, 75)
        Me.button2.TabIndex = 6
        Me.button2.Text = "2"
        "
        "button3
        "
        Me.button3.Location = New System.Drawing.Point(168, 8)
        Me.button3.Name = "button3"
        Me.button3.Size = New System.Drawing.Size(75, 75)
        Me.button3.TabIndex = 9
        Me.button3.Text = "3"
        "
        "button4
        "
        Me.button4.Location = New System.Drawing.Point(8, 88)
        Me.button4.Name = "button4"
        Me.button4.Size = New System.Drawing.Size(75, 75)
        Me.button4.TabIndex = 8
        Me.button4.Text = "4"
        "
        "button5
        "
        Me.button5.Location = New System.Drawing.Point(88, 88)
        Me.button5.Name = "button5"
        Me.button5.Size = New System.Drawing.Size(75, 75)
        Me.button5.TabIndex = 5
        Me.button5.Text = "5"
        "
        "button6
        "
        Me.button6.Location = New System.Drawing.Point(168, 88)
        Me.button6.Name = "button6"
        Me.button6.Size = New System.Drawing.Size(75, 75)
        Me.button6.TabIndex = 2
        Me.button6.Text = "6"
        "
        "button7
        "
        Me.button7.Location = New System.Drawing.Point(8, 168)
        Me.button7.Name = "button7"
        Me.button7.Size = New System.Drawing.Size(75, 75)
        Me.button7.TabIndex = 1
        Me.button7.Text = "7"
        "
        "button8
        "
        Me.button8.Location = New System.Drawing.Point(88, 168)
        Me.button8.Name = "button8"
        Me.button8.Size = New System.Drawing.Size(75, 75)
        Me.button8.TabIndex = 4
        Me.button8.Text = "8"
        "
        "button9
        "
        Me.button9.Location = New System.Drawing.Point(168, 168)
        Me.button9.Name = "button9"
        Me.button9.Size = New System.Drawing.Size(75, 75)
        Me.button9.TabIndex = 3
        Me.button9.Text = "9"
        "
        "Form1
        "
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(248, 246)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.button1, Me.button2, Me.button3, Me.button4, Me.button5, Me.button6, Me.button7, Me.button8, Me.button9})
        Me.Name = "Form1"
        Me.Text = "Grid Layout Example"
        Me.ResumeLayout(False)
    End Sub
#End Region
    Private Sub GridForm_Layout(ByVal sender As Object, ByVal e As System.Windows.Forms.LayoutEventArgs) Handles MyBase.Layout
        Dim buttons As Button() = New Button() {button1, button2, button3, button4, button5, button6, button7, button8, button9}
        Dim cx As Integer = ClientRectangle.Width / 3
        Dim cy As Integer = ClientRectangle.Height / 3
        Dim row As Integer = 0
        Do While row < 3
            Dim col As Integer = 0
            Do While col < 3
                Dim b As Button = buttons(col * 3 + row)
                b.Text = "Button"
                b.SetBounds(cx * row, cy * col, cx, cy)
                col = col + 1
            Loop
            row = row + 1
        Loop
        SetClientSizeCore(cx * 3, cy * 3)
    End Sub
End Class

Form mouse event: Mouse Enter, Mouse Down, Mouse Hover, Mouse, Move, Mouse Up, Mouse Wheel, Mouse Leave

Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class FormMouseEvent
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class
Public Class Form1
    Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
    Public Sub New()
        MyBase.New()
        "This call is required by the Windows Form Designer.
        InitializeComponent()
        "Add any initialization after the InitializeComponent() call
    End Sub
    "Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub
    "Required by the Windows Form Designer
    Private components As System.ruponentModel.IContainer
    "NOTE: The following procedure is required by the Windows Form Designer
    "It can be modified using the Windows Form Designer.  
    "Do not modify it using the code editor.
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.SuspendLayout()
        "
        "TextBox1
        "
        Me.TextBox1.Location = New System.Drawing.Point(16, 48)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.Size = New System.Drawing.Size(256, 20)
        Me.TextBox1.TabIndex = 0
        Me.TextBox1.Text = ""
        "
        "Form1
        "
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 273)
        Me.Controls.Add(Me.TextBox1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)
    End Sub
#End Region
    Private Sub Form1_MouseEnter(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles MyBase.MouseEnter
        TextBox1.Text = "The mouse entered the client area."
    End Sub
    Private Sub Form1_MouseDown(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.MouseEventArgs) _
    Handles MyBase.MouseDown
        If e.Button = MouseButtons.Left Then
            TextBox1.Text = "The left mouse button is down at (" & CStr(e.X) & ", " & CStr(e.Y) & ")"
        End If
    End Sub
    Private Sub Form1_MouseHover(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles MyBase.MouseHover
        TextBox1.Text = "The mouse is hovering."
    End Sub
    Private Sub Form1_MouseMove(ByVal sender As Object, _
        ByVal e As System.Windows.Forms.MouseEventArgs) _
        Handles MyBase.MouseMove
        TextBox1.Text = "The mouse moved to: (" & CStr(e.X) & ", " & CStr(e.Y) & ")"
    End Sub
    Private Sub Form1_MouseUp(ByVal sender As Object, _
        ByVal e As System.Windows.Forms.MouseEventArgs) _
        Handles MyBase.MouseUp
        If e.Button = MouseButtons.Left Then
            TextBox1.Text = "The left mouse button went up at (" & CStr(e.X) & ", " & CStr(e.Y) & ")"
        End If
    End Sub
    Private Sub Form1_MouseWheel(ByVal sender As Object, _
        ByVal e As System.Windows.Forms.MouseEventArgs) _
        Handles MyBase.MouseWheel
        TextBox1.Text = "The mouse wheel rotated " & CStr(e.Delta) & " detents"
    End Sub
    Private Sub Form1_MouseLeave(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles MyBase.MouseLeave
        TextBox1.Text = "The mouse left the client area."
    End Sub
End Class

Form resize event

Imports System.Windows.Forms
public class FormResizeEvent
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class
Public Class Form1
    Private Sub Form1_Resize(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Resize
        GroupBox1.Top = 0
        GroupBox1.Left = 0
        GroupBox1.Width = Me.Width
        GroupBox1.Height = Me.Height
        Label1.Left = GroupBox1.Top + 10
        Label1.Top = GroupBox1.Left + 20
        TextBox1.Top = Label1.Top + 50
        TextBox1.Left = Label1.Left
        TextBox1.Width = GroupBox1.Width - 20
    End Sub
End Class
<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form
    "Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        End If
        MyBase.Dispose(disposing)
    End Sub
    "Required by the Windows Form Designer
    Private components As System.ruponentModel.IContainer
    "NOTE: The following procedure is required by the Windows Form Designer
    "It can be modified using the Windows Form Designer.  
    "Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.GroupBox1 = New System.Windows.Forms.GroupBox
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.Label1 = New System.Windows.Forms.Label
        Me.GroupBox1.SuspendLayout()
        Me.SuspendLayout()
        "
        "GroupBox1
        "
        Me.GroupBox1.Controls.Add(Me.TextBox1)
        Me.GroupBox1.Controls.Add(Me.Label1)
        Me.GroupBox1.Location = New System.Drawing.Point(12, 12)
        Me.GroupBox1.Name = "GroupBox1"
        Me.GroupBox1.Size = New System.Drawing.Size(392, 195)
        Me.GroupBox1.TabIndex = 0
        Me.GroupBox1.TabStop = False
        Me.GroupBox1.Text = "GroupBox1"
        "
        "TextBox1
        "
        Me.TextBox1.Location = New System.Drawing.Point(32, 62)
        Me.TextBox1.Multiline = True
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.Size = New System.Drawing.Size(331, 21)
        Me.TextBox1.TabIndex = 5
        "
        "Label1
        "
        Me.Label1.AutoSize = True
        Me.Label1.Location = New System.Drawing.Point(30, 30)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(41, 12)
        Me.Label1.TabIndex = 4
        Me.Label1.Text = "Label1"
        "
        "Form1
        "
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 12.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(416, 219)
        Me.Controls.Add(Me.GroupBox1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.GroupBox1.ResumeLayout(False)
        Me.GroupBox1.PerformLayout()
        Me.ResumeLayout(False)
    End Sub
    Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents Label1 As System.Windows.Forms.Label
End Class

Form Resize: fixed ratio

"Visual Basic 2005 Programmer"s Reference
"by Rod Stephens (Author) 
"# Publisher: Wrox (October 21, 2005)
"# Language: English
"# ISBN-10: 0764571982
"# ISBN-13: 978-0764571985
Imports System.Runtime.InteropServices
Imports System.Windows.Forms
public class FixedRatio
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class

Public Class Form1
    Public Structure Rect
        Public left As Integer
        Public top As Integer
        Public right As Integer
        Public bottom As Integer
    End Structure
    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        Const WM_SIZING As Long = &H214
        Const WMSZ_LEFT As Integer = 1
        Const WMSZ_RIGHT As Integer = 2
        Const WMSZ_TOP As Integer = 3
        Const WMSZ_TOPLEFT As Integer = 4
        Const WMSZ_TOPRIGHT As Integer = 5
        Const WMSZ_BOTTOM As Integer = 6
        Const WMSZ_BOTTOMLEFT As Integer = 7
        Const WMSZ_BOTTOMRIGHT As Integer = 8
        Static fixed_aspect_ratio As Double = 0
        Dim new_aspect_ratio As Double
        If m.Msg = WM_SIZING And m.HWnd.Equals(Me.Handle) Then
            " Turn the message&iexcl;�s lParam into a Rect.
            Dim r As Rect
            r = DirectCast( _
            Marshal.PtrToStructure(m.LParam, GetType(Rect)), _
            Rect)
            " Get the current dimensions.
            Dim wid As Double = r.right - r.left
            Dim hgt As Double = r.bottom - r.top
            " Get the new aspect ratio.
            new_aspect_ratio = hgt / wid
            " The first time, save the form&iexcl;�s aspect ratio.
            If fixed_aspect_ratio = 0 Then
                fixed_aspect_ratio = new_aspect_ratio
            End If
            " See if the aspect ratio is changing.
            If fixed_aspect_ratio <> new_aspect_ratio Then
                " To decide which dimension we should preserve,
                " see what border the user is dragging.
                If m.WParam.ToInt32 = WMSZ_TOPLEFT Or _
                m.WParam.ToInt32 = WMSZ_TOPRIGHT Or _
                m.WParam.ToInt32 = WMSZ_BOTTOMLEFT Or _
                m.WParam.ToInt32 = WMSZ_BOTTOMRIGHT _
                Then
                    " The user is dragging a corner.
                    " Preserve the bigger dimension.
                    If new_aspect_ratio > fixed_aspect_ratio Then
                        " It&iexcl;�s too tall and thin. Make it wider.
                        wid = hgt / fixed_aspect_ratio
                    Else
                        " It&iexcl;�s too short and wide. Make it taller.
                        hgt = wid * fixed_aspect_ratio
                    End If
                ElseIf m.WParam.ToInt32 = WMSZ_LEFT Or _
                m.WParam.ToInt32 = WMSZ_RIGHT _
                Then
                    " The user is dragging a side.
                    " Preserve the width.
                    hgt = wid * fixed_aspect_ratio
                ElseIf m.WParam.ToInt32 = WMSZ_TOP Or _
                m.WParam.ToInt32 = WMSZ_BOTTOM _
                Then
                    " The user is dragging the top or bottom.
                    " Preserve the height.
                    wid = hgt / fixed_aspect_ratio
                End If
                " Figure out whether to reset the top/bottom
                " and left/right.
                " See if the user is dragging the top edge.
                If m.WParam.ToInt32 = WMSZ_TOP Or _
                m.WParam.ToInt32 = WMSZ_TOPLEFT Or _
                m.WParam.ToInt32 = WMSZ_TOPRIGHT _
                Then
                    " Reset the top.
                    r.top = r.bottom - CInt(hgt)
                Else
                    " Reset the bottom.
                    r.bottom = r.top + CInt(hgt)
                End If
                " See if the user is dragging the left edge.
                If m.WParam.ToInt32 = WMSZ_LEFT Or _
                m.WParam.ToInt32 = WMSZ_TOPLEFT Or _
                m.WParam.ToInt32 = WMSZ_BOTTOMLEFT _
                Then
                    " Reset the left.
                    r.left = r.right - CInt(wid)
                Else
                    " Reset the right.
                    r.right = r.left + CInt(wid)
                End If
                " Update the Message object&iexcl;�s LParam field.
                Marshal.StructureToPtr(r, m.LParam, True)
            End If
        End If
        MyBase.WndProc(m)
    End Sub
End Class
<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _
Partial Public Class Form1
    Inherits System.Windows.Forms.Form
    "Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        End If
        MyBase.Dispose(disposing)
    End Sub
    "Required by the Windows Form Designer
    Private components As System.ruponentModel.IContainer
    "NOTE: The following procedure is required by the Windows Form Designer
    "It can be modified using the Windows Form Designer.  
    "Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.SuspendLayout()
        "
        "Form1
        "
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(292, 273)
        Me.Name = "Form1"
        Me.Text = "FixedAspectRatio"
        Me.ResumeLayout(False)
    End Sub
End Class

Message Filter

Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
Imports System.Math
public class MessageFilter
   public Shared Sub Main
         Application.Run(New Form1)
   End Sub
End class
Public Class Form1
    Public Class NoLeftDownMessageFilter
        Implements IMessageFilter
        Public Function PreFilterMessage(ByRef m As System.Windows.Forms.Message) As Boolean Implements IMessageFilter.PreFilterMessage
            Const WM_LBUTTONDOWN As Long = &H201
            Return (m.Msg = WM_LBUTTONDOWN)
        End Function
    End Class
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim no_left_down_message_filter As New NoLeftDownMessageFilter
        Application.AddMessageFilter(no_left_down_message_filter)
    End Sub
    Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
        Application.UseWaitCursor = Not Application.UseWaitCursor
    End Sub
End Class
<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _
Partial Public Class Form1
    Inherits System.Windows.Forms.Form
    "Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        End If
        MyBase.Dispose(disposing)
    End Sub
    "Required by the Windows Form Designer
    Private components As System.ruponentModel.IContainer
    "NOTE: The following procedure is required by the Windows Form Designer
    "It can be modified using the Windows Form Designer.  
    "Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.SuspendLayout()
        "
        "Form1
        "
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(292, 273)
        Me.Name = "Form1"
        Me.Text = "UseMessageFilter"
        Me.ResumeLayout(False)
    End Sub
End Class

Overrides OnNotifyMessage method

Imports System.Windows.Forms
Imports System.IO

Public Class WndProcTextBox
    Inherits TextBox
#Region " Component Designer generated code "
    Public Sub New(ByVal Container As System.ruponentModel.IContainer)
        MyClass.New()
        "Required for Windows.Forms Class Composition Designer support
        Container.Add(Me)
    End Sub
    Public Sub New()
        MyBase.New()
        "This call is required by the Component Designer.
        InitializeComponent()
        "Add any initialization after the InitializeComponent() call
        Me.SetStyle(ControlStyles.EnableNotifyMessage, True)
    End Sub
    "Component overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub
    "Required by the Component Designer
    Private components As System.ruponentModel.IContainer
    "NOTE: The following procedure is required by the Component Designer
    "It can be modified using the Component Designer.
    "Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        components = New System.ruponentModel.Container()
    End Sub
#End Region
    Public Delegate Sub ShowEventInfo(ByVal info As String)
    Public Event ShowEventInfoEvent As ShowEventInfo

    Protected Overrides Sub OnNotifyMessage(ByVal m As System.Windows.Forms.Message)
        RaiseEvent ShowEventInfoEvent(String.Format("Msg: HWND({0}) LPARAM({1}) WPARAM({2}) MSG({3})", m.HWnd, m.LParam, m.WParam, m.Msg))
        MyBase.OnNotifyMessage(m)
    End Sub
End Class

Show Form Message

Imports System.Windows.Forms
public class OverrideWndProc
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class
Public Class Form1
    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        Console.WriteLine(m.ToString)
        MyBase.WndProc(m)
    End Sub
End Class
<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _
Partial Public Class Form1
    Inherits System.Windows.Forms.Form
    "Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        End If
        MyBase.Dispose(disposing)
    End Sub
    "Required by the Windows Form Designer
    Private components As System.ruponentModel.IContainer
    "NOTE: The following procedure is required by the Windows Form Designer
    "It can be modified using the Windows Form Designer.  
    "Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.SuspendLayout()
        "
        "Form1
        "
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(292, 273)
        Me.Name = "Form1"
        Me.Text = "ShowMessages"
        Me.ResumeLayout(False)
    End Sub
End Class