Option Strict On ' a good idea - if you like Option Explicit in VB6 you'll love this
Public Class frmKeyboard
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.ComponentModel.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 btnA As System.Windows.Forms.Button
Friend WithEvents btnB As System.Windows.Forms.Button
Friend WithEvents btnClose As System.Windows.Forms.Button
Friend WithEvents txtOutput As System.Windows.Forms.TextBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.btnA = New System.Windows.Forms.Button()
Me.btnB = New System.Windows.Forms.Button()
Me.btnClose = New System.Windows.Forms.Button()
Me.txtOutput = New System.Windows.Forms.TextBox()
Me.SuspendLayout()
'
'btnA
'
Me.btnA.Location = New System.Drawing.Point(32, 48)
Me.btnA.Name = "btnA"
Me.btnA.TabIndex = 0
Me.btnA.Tag = "Key"
Me.btnA.Text = "&A"
'
'btnB
'
Me.btnB.Location = New System.Drawing.Point(112, 48)
Me.btnB.Name = "btnB"
Me.btnB.TabIndex = 1
Me.btnB.Tag = "Key"
Me.btnB.Text = "&B"
'
'btnClose
'
Me.btnClose.Location = New System.Drawing.Point(80, 232)
Me.btnClose.Name = "btnClose"
Me.btnClose.TabIndex = 2
Me.btnClose.Text = "&Close"
'
'txtOutput
'
Me.txtOutput.Location = New System.Drawing.Point(32, 88)
Me.txtOutput.Multiline = True
Me.txtOutput.Name = "txtOutput"
Me.txtOutput.Size = New System.Drawing.Size(200, 104)
Me.txtOutput.TabIndex = 3
Me.txtOutput.Text = ""
'
'frmKeyboard
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.txtOutput, Me.btnClose, Me.btnB, Me.btnA})
Me.Name = "frmKeyboard"
Me.Text = "frmKeyboard"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub frmKeyboard_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'put the click event for each button which has a tag of "Key" into our handler
Dim ctl As Control
For Each ctl In Me.Controls
'there's a lot of .net stuff here
'AndAlso - the part after will not be executed unless it is true -
'in VB6 both parts are executed regardless and this would cause an error if the first was not true - you'd need a nested if to avoid this
If Not ctl.Tag Is Nothing AndAlso ctl.Tag.ToString.ToLower = "Key".ToLower Then
'in .net, even a string literal is an object and has methods.
AddHandler ctl.Click, AddressOf KeyClick
End If
Next
'I'm putting another event in here for the B key just to show that in .net, an event can be handled by two different subs
'the one caveat is that you don't know which will fire first, but hey!
AddHandler btnB.Click, AddressOf ShowMessage
End Sub
Private Sub KeyClick(ByVal sender As Object, ByVal e As EventArgs)
Dim btn As Button
Try 'in .net, we get structured error handling
btn = CType(sender, Button) 'in .net, we get inheritance - button inherits from control - impossible in vb6
Catch
Throw New Exception("You've assigned a non button control to the KeyClick event") 'inline constructors for classes - both impossible in VB6
End Try
'we have to chop off the & from the text of the button (the & is for the accelerator key)
'there are lots of ways to do this, easiest is to use the char datatype (a ByValue datatype) (not present in VB6)
Dim c As String = btn.Text.Chars(1)
txtOutput.Text &= c 'shorthand concatenation &= - we get this in .net too
End Sub
Private Sub ShowMessage(ByVal sender As Object, ByVal e As EventArgs)
MessageBox.Show("You pressed button " & sender.ToString) 'every object has a ToString method
End Sub
Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
Me.Dispose() 'destructors - again, not present in VB6
End Sub
End Class