VB.Net Tutorial/Event/Key Event

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

Check Key Code: F5

<source lang="vbnet">Imports System.Windows.Forms public class KeysCodeF5

  public Shared Sub Main
       Application.Run(New Form1)
  End Sub

End class

Public Class Form1

   Private Sub Form1_KeyDown(ByVal sender As Object, _
         ByVal e As System.Windows.Forms.KeyEventArgs) _
         Handles Me.KeyDown
       If (e.KeyCode = Keys.F5) Then Console.WriteLine("Form: F5")
       e.Handled = True
   End Sub
   Private Sub TextBox1_KeyDown(ByVal sender As Object, _
         ByVal e As System.Windows.Forms.KeyEventArgs) _
         Handles TextBox1.KeyDown
       If (e.KeyCode = Keys.F5) Then Console.WriteLine("Text: F5")
   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.TextBox1 = New System.Windows.Forms.TextBox
       Me.SuspendLayout()
       "
       "TextBox1
       "
       Me.TextBox1.Location = New System.Drawing.Point(16, 16)
       Me.TextBox1.Name = "TextBox1"
       Me.TextBox1.Size = New System.Drawing.Size(328, 20)
       Me.TextBox1.TabIndex = 0
       "
       "Form1
       "
       Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
       Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
       Me.ClientSize = New System.Drawing.Size(385, 162)
       Me.Controls.Add(Me.TextBox1)
       Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
       Me.KeyPreview = True
       Me.MaximizeBox = False
       Me.Name = "Form1"
       Me.Text = "Intercepting Form-level Keys"
       Me.ResumeLayout(False)
       Me.PerformLayout()
   End Sub
   Friend WithEvents TextBox1 As System.Windows.Forms.TextBox

End Class</source>

Check Key Code range

<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class KeyCodeRangeShift

  public Shared Sub Main
       Application.Run(New Form1)
  End Sub

End class Public Class Form1

   Inherits System.Windows.Forms.Form
  1. 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 Label1 As System.Windows.Forms.Label
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
       Me.Label1 = New System.Windows.Forms.Label
       Me.SuspendLayout()
       "
       "Label1
       "
       Me.Label1.Location = New System.Drawing.Point(56, 80)
       Me.Label1.Name = "Label1"
       Me.Label1.Size = New System.Drawing.Size(184, 200)
       Me.Label1.TabIndex = 0
       "
       "Form1
       "
       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
       Me.ClientSize = New System.Drawing.Size(292, 273)
       Me.Controls.Add(Me.Label1)
       Me.Name = "Form1"
       Me.Text = "Form1"
       Me.ResumeLayout(False)
   End Sub
  1. End Region
   Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
       If e.KeyCode >= Keys.A And e.KeyCode <= Keys.Z Then
           If e.Shift Then
               Label1.Text &= Chr(e.KeyCode)
           Else
               Label1.Text &= Char.ToLower(Chr(e.KeyCode))
           End If
       End If
   End Sub
   Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
       Label1.Text &= e.KeyChar
   End Sub

End Class</source>

Control Key

<source lang="vbnet">Imports System.Windows.Forms public class ControlKey

  public Shared Sub Main
       Application.Run(New Form1)
  End Sub

End class Public Class Form1

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       TextBox1.Text = TextBox1.Text + TextBox2.Text
       TextBox2.Text = ""
   End Sub
   Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
       TextBox2.Text = ""
   End Sub
   Private Sub TextBox2_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox2.KeyDown
       Dim res As MsgBoxResult
       Select Case e.KeyCode
           Case Keys.Escape
                TextBox2.Text = ""
           Case Keys.Enter
                TextBox2.Text = ""
           Case Keys.Control
               Select Case e.KeyData
                   Case Keys.A
                       TextBox2.SelectionStart = 0
                       TextBox2.SelectionLength = Len(TextBox2.Text)
                   Case Keys.C
                       Clipboard.SetDataObject(TextBox2.SelectedText)
                   Case Keys.V
                       Dim iData As IDataObject
                       iData = Clipboard.GetDataObject()
                       If (iData.GetDataPresent(DataFormats.Text)) Then
                           TextBox2.SelectedText = iData.GetData(DataFormats.Text)
                       Else
                           MsgBox("Could not retrieve data off the clipboard.")
                       End If
                   Case Keys.X
                       Clipboard.SetDataObject(TextBox2.SelectedText)
                       TextBox2.SelectedText = ""
               End Select
           Case Keys.F1
               Console.WriteLine("F1")
       End Select
   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.Button1 = New System.Windows.Forms.Button
       Me.Button2 = New System.Windows.Forms.Button
       Me.Label1 = New System.Windows.Forms.Label
       Me.Label2 = New System.Windows.Forms.Label
       Me.TextBox1 = New System.Windows.Forms.TextBox
       Me.TextBox2 = New System.Windows.Forms.TextBox
       Me.SuspendLayout()
       "
       "Button1
       "
       Me.Button1.Location = New System.Drawing.Point(101, 205)
       Me.Button1.Name = "Button1"
       Me.Button1.Size = New System.Drawing.Size(75, 23)
       Me.Button1.TabIndex = 0
       Me.Button1.Text = "Add"
       Me.Button1.UseVisualStyleBackColor = True
       "
       "Button2
       "
       Me.Button2.Location = New System.Drawing.Point(291, 205)
       Me.Button2.Name = "Button2"
       Me.Button2.Size = New System.Drawing.Size(75, 23)
       Me.Button2.TabIndex = 1
       Me.Button2.Text = "Cancel"
       Me.Button2.UseVisualStyleBackColor = True
       "
       "Label1
       "
       Me.Label1.AutoSize = True
       Me.Label1.Location = New System.Drawing.Point(24, 22)
       Me.Label1.Name = "Label1"
       Me.Label1.Size = New System.Drawing.Size(53, 12)
       Me.Label1.TabIndex = 2
       Me.Label1.Text = "Old"
       "
       "Label2
       "
       Me.Label2.AutoSize = True
       Me.Label2.Location = New System.Drawing.Point(24, 134)
       Me.Label2.Name = "Label2"
       Me.Label2.Size = New System.Drawing.Size(53, 12)
       Me.Label2.TabIndex = 3
       Me.Label2.Text = "New"
       "
       "TextBox1
       "
       Me.TextBox1.Location = New System.Drawing.Point(35, 37)
       Me.TextBox1.Multiline = True
       Me.TextBox1.Name = "TextBox1"
       Me.TextBox1.Size = New System.Drawing.Size(394, 94)
       Me.TextBox1.TabIndex = 4
       "
       "TextBox2
       "
       Me.TextBox2.Location = New System.Drawing.Point(37, 159)
       Me.TextBox2.Name = "TextBox2"
       Me.TextBox2.Size = New System.Drawing.Size(392, 21)
       Me.TextBox2.TabIndex = 5
       "
       "Form1
       "
       Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 12.0!)
       Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
       Me.ClientSize = New System.Drawing.Size(468, 266)
       Me.Controls.Add(Me.TextBox2)
       Me.Controls.Add(Me.TextBox1)
       Me.Controls.Add(Me.Label2)
       Me.Controls.Add(Me.Label1)
       Me.Controls.Add(Me.Button2)
       Me.Controls.Add(Me.Button1)
       Me.Name = "Form1"
       Me.Text = "Form1"
       Me.ResumeLayout(False)
       Me.PerformLayout()
   End Sub
   Friend WithEvents Button1 As System.Windows.Forms.Button
   Friend WithEvents Button2 As System.Windows.Forms.Button
   Friend WithEvents Label1 As System.Windows.Forms.Label
   Friend WithEvents Label2 As System.Windows.Forms.Label
   Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
   Friend WithEvents TextBox2 As System.Windows.Forms.TextBox

End Class</source>

Determine whether user pressed Enter key

<source lang="vbnet">Imports System.Windows.Forms

public class KeyCodeEnter

  public Shared Sub Main
       Application.Run(New FrmFileTest)
  End Sub

End class Public Class FrmFileTest

  Inherits Form
  " label that gives directions to user
  Friend WithEvents lblDirections As Label
  " text boxes for inputting and outputting data
  Friend WithEvents txtOutput As TextBox
  Friend WithEvents txtInput As TextBox
  1. 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.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.lblDirections = New System.Windows.Forms.Label()
     Me.txtOutput = New System.Windows.Forms.TextBox()
     Me.txtInput = New System.Windows.Forms.TextBox()
     Me.SuspendLayout()
     "
     "lblDirections
     "
     Me.lblDirections.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
     Me.lblDirections.Location = New System.Drawing.Point(20, 20)
     Me.lblDirections.Name = "lblDirections"
     Me.lblDirections.Size = New System.Drawing.Size(410, 19)
     Me.lblDirections.TabIndex = 2
     Me.lblDirections.Text = "Press Enter key:"
     Me.lblDirections.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
     "
     "txtOutput
     "
     Me.txtOutput.AutoSize = False
     Me.txtOutput.BackColor = System.Drawing.SystemColors.Control
     Me.txtOutput.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
     Me.txtOutput.Location = New System.Drawing.Point(20, 109)
     Me.txtOutput.Multiline = True
     Me.txtOutput.Name = "txtOutput"
     Me.txtOutput.ReadOnly = True
     Me.txtOutput.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
     Me.txtOutput.Size = New System.Drawing.Size(410, 345)
     Me.txtOutput.TabIndex = 1
     Me.txtOutput.Text = ""
     "
     "txtInput
     "
     Me.txtInput.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
     Me.txtInput.Location = New System.Drawing.Point(20, 59)
     Me.txtInput.Name = "txtInput"
     Me.txtInput.Size = New System.Drawing.Size(410, 26)
     Me.txtInput.TabIndex = 0
     Me.txtInput.Text = ""
     "
     "FrmFileTest
     "
     Me.AutoScaleBaseSize = New System.Drawing.Size(6, 15)
     Me.ClientSize = New System.Drawing.Size(460, 470)
     Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.txtOutput, Me.lblDirections, Me.txtInput})
     Me.Name = "FrmFileTest"
     Me.Text = "File Test"
     Me.ResumeLayout(False)
  End Sub
  1. End Region
  " invoked when user presses key
  Protected Sub txtInput_KeyDown(ByVal sender As Object, _
     ByVal e As System.Windows.Forms.KeyEventArgs) Handles _
     txtInput.KeyDown
     
     If e.KeyCode = Keys.Enter Then
         Console.WriteLine("Enter")
     End If 
  End Sub 

End Class</source>

Displaying information about a user-pressed key

<source lang="vbnet">Imports System.Windows.Forms public class KeyCodeChar

  public Shared Sub Main
       Application.Run(New FrmKeyDemo)
  End Sub

End class

Public Class FrmKeyDemo

  Inherits System.Windows.Forms.Form
  1. 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
  Friend WithEvents lblInformation As System.Windows.Forms.Label
  Friend WithEvents lblCharacter As System.Windows.Forms.Label
  "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.lblInformation = New System.Windows.Forms.Label()
     Me.lblCharacter = New System.Windows.Forms.Label()
     Me.SuspendLayout()
     "
     "lblInformation
     "
     Me.lblInformation.Font = New System.Drawing.Font("Microsoft Sans Serif", 12!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
     Me.lblInformation.Location = New System.Drawing.Point(8, 56)
     Me.lblInformation.Name = "lblInformation"
     Me.lblInformation.Size = New System.Drawing.Size(176, 136)
     Me.lblInformation.TabIndex = 1
     "
     "lblCharacter
     "
     Me.lblCharacter.Font = New System.Drawing.Font("Microsoft Sans Serif", 12!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
     Me.lblCharacter.Location = New System.Drawing.Point(8, 16)
     Me.lblCharacter.Name = "lblCharacter"
     Me.lblCharacter.Size = New System.Drawing.Size(168, 23)
     Me.lblCharacter.TabIndex = 0
     "
     "FrmKeyDemo
     "
     Me.AutoScaleBaseSize = New System.Drawing.Size(9, 22)
     Me.ClientSize = New System.Drawing.Size(192, 205)
     Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.lblInformation, Me.lblCharacter})
     Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 14.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
     Me.Name = "FrmKeyDemo"
     Me.Text = "KeyDemo"
     Me.ResumeLayout(False)
  End Sub
  1. End Region
  Private Sub FrmKeyDemo_KeyPress(ByVal sender As System.Object, _
     ByVal e As System.Windows.Forms.KeyPressEventArgs) _
     Handles MyBase.KeyPress
     lblCharacter.Text = "Key pressed: " & e.KeyChar
  End Sub
  Private Sub FrmKeyDemo_KeyDown(ByVal sender As System.Object, _
     ByVal e As System.Windows.Forms.KeyEventArgs) _
     Handles MyBase.KeyDown
     lblInformation.Text = "KeyCode: " & e.KeyCode.ToString & _
           vbCrLf & "KeyData: " & e.KeyData.ToString & _
           vbCrLf & "KeyValue: " & e.KeyValue & vbCrLf
     If e.Alt Then
        lblInformation.Text &= "Alt: Yes" & vbCrLf
     Else
        lblInformation.Text &= "Alt: No" & vbCrLf
     End If
     If e.Shift Then
        lblInformation.Text &= "Shift: Yes" & vbCrLf
     Else
        lblInformation.Text &= "Shift: No" & vbCrLf
     End If
     If e.Control Then
        lblInformation.Text &= "Ctrl: Yes" & vbCrLf
     Else
        lblInformation.Text &= "Ctrl: No" & vbCrLf
     End If
  End Sub " FrmKeyDemo_KeyDown
  " clear labels when key is released
  Private Sub FrmKeyDemo_KeyUp(ByVal sender As System.Object, _
     ByVal e As System.Windows.Forms.KeyEventArgs) _
     Handles MyBase.KeyUp
     lblInformation.Text = "Key up"
     lblCharacter.Text = "Key up"
  End Sub 

End Class</source>

Ignore key code

<source lang="vbnet">Imports System.Windows.Forms public class TextBoxValidating

  public Shared Sub Main
       Application.Run(New Form1)
  End Sub

End class Public Class Form1

   Inherits System.Windows.Forms.Form
  1. 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 Label1 As System.Windows.Forms.Label
  Friend WithEvents txtInput As System.Windows.Forms.TextBox
  Friend WithEvents Label2 As System.Windows.Forms.Label
  Friend WithEvents Label3 As System.Windows.Forms.Label
  Friend WithEvents lblTrue As System.Windows.Forms.Label
  Friend WithEvents lblCheck As System.Windows.Forms.Label
  Friend WithEvents lblResults As System.Windows.Forms.Label
  Friend WithEvents btnClear As System.Windows.Forms.Button
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

Me.Label1 = New System.Windows.Forms.Label() Me.txtInput = New System.Windows.Forms.TextBox() Me.Label2 = New System.Windows.Forms.Label() Me.Label3 = New System.Windows.Forms.Label() Me.lblTrue = New System.Windows.Forms.Label() Me.lblCheck = New System.Windows.Forms.Label() Me.lblResults = New System.Windows.Forms.Label() Me.btnClear = New System.Windows.Forms.Button() Me.SuspendLayout() " "Label1 " Me.Label1.Font = New System.Drawing.Font("Tahoma", 14.25!, (System.Drawing.FontStyle.Bold Or System.Drawing.FontStyle.Italic), System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Label1.Location = New System.Drawing.Point(48, 16) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(176, 23) Me.Label1.TabIndex = 0 Me.Label1.Text = "ISBN Validation" " "txtInput " Me.txtInput.Location = New System.Drawing.Point(72, 64) Me.txtInput.Name = "txtInput" Me.txtInput.TabIndex = 1 Me.txtInput.Text = "" " "Label2 " Me.Label2.Location = New System.Drawing.Point(24, 104) Me.Label2.Name = "Label2" Me.Label2.Size = New System.Drawing.Size(80, 23) Me.Label2.TabIndex = 2 Me.Label2.Text = "True Number:" " "Label3 " Me.Label3.Location = New System.Drawing.Point(32, 152) Me.Label3.Name = "Label3" Me.Label3.Size = New System.Drawing.Size(72, 23) Me.Label3.TabIndex = 3 Me.Label3.Text = "Check Digit:" " "lblTrue " Me.lblTrue.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.lblTrue.Location = New System.Drawing.Point(112, 104) Me.lblTrue.Name = "lblTrue" Me.lblTrue.TabIndex = 4 " "lblCheck " Me.lblCheck.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.lblCheck.Location = New System.Drawing.Point(112, 152) Me.lblCheck.Name = "lblCheck" Me.lblCheck.TabIndex = 5 " "lblResults " Me.lblResults.Location = New System.Drawing.Point(56, 192) Me.lblResults.Name = "lblResults" Me.lblResults.Size = New System.Drawing.Size(152, 24) Me.lblResults.TabIndex = 6 " "btnClear " Me.btnClear.Location = New System.Drawing.Point(88, 240) Me.btnClear.Name = "btnClear" Me.btnClear.TabIndex = 7 Me.btnClear.Text = "Clear" " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(264, 293) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnClear, Me.lblResults, Me.lblCheck, Me.lblTrue, Me.Label3, Me.Label2, Me.txtInput, Me.Label1}) Me.Name = "Form1" Me.Text = "ISBN Validation" Me.ResumeLayout(False)

   End Sub
  1. End Region
  Private Sub IsbnValidation(ByVal sender As Object, ByVal e As System.ruponentModel.CancelEventArgs) Handles txtInput.Validating
        Console.WriteLine("validating")
  End Sub
  Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
     txtInput.Text = ""
     lblResults.Text = ""
     lblTrue.Text = ""
     lblCheck.Text = ""
  End Sub
  Private Sub txtInput_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtInput.KeyPress
     Dim keyChar As Char
     keyChar = e.KeyChar
     
     " Suppress any keys except digits,X,x,hyphen (45),Backspace (8),or Enter (13)
     If ((Not Char.IsDigit(keyChar)) _
        And (AscW(keyChar) <> 8) _
        And (AscW(keyChar) <> 13) _
        And (keyChar <> "X"c) _
        And (keyChar <> "x"c) _
        And (AscW(keyChar) <> 45)) Then
        "  Do not display the keystroke
        e.Handled = True
     End If
  End Sub

End Class</source>

Key event summary

<source lang="vbnet">Imports System.Windows.Forms public class KeyEventSummary

  public Shared Sub Main
       Application.Run(New Form1)
  End Sub

End class Public Class Form1

   Inherits System.Windows.Forms.Form
  1. 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 txtInput As System.Windows.Forms.TextBox
   Friend WithEvents txtMsg As System.Windows.Forms.TextBox
   Friend WithEvents btnReset As System.Windows.Forms.Button
   Friend WithEvents Label1 As System.Windows.Forms.Label
   Friend WithEvents Label2 As System.Windows.Forms.Label
   Friend WithEvents lblUpper As System.Windows.Forms.Label
   Friend WithEvents lblLower As System.Windows.Forms.Label
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
       Me.txtInput = New System.Windows.Forms.TextBox()
       Me.txtMsg = New System.Windows.Forms.TextBox()
       Me.btnReset = New System.Windows.Forms.Button()
       Me.Label1 = New System.Windows.Forms.Label()
       Me.Label2 = New System.Windows.Forms.Label()
       Me.lblUpper = New System.Windows.Forms.Label()
       Me.lblLower = New System.Windows.Forms.Label()
       Me.SuspendLayout()
       "
       "txtInput
       "
       Me.txtInput.Location = New System.Drawing.Point(8, 8)
       Me.txtInput.Name = "txtInput"
       Me.txtInput.TabIndex = 0
       Me.txtInput.Text = ""
       "
       "txtMsg
       "
       Me.txtMsg.Location = New System.Drawing.Point(8, 40)
       Me.txtMsg.Multiline = True
       Me.txtMsg.Name = "txtMsg"
       Me.txtMsg.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
       Me.txtMsg.Size = New System.Drawing.Size(304, 232)
       Me.txtMsg.TabIndex = 1
       Me.txtMsg.TabStop = False
       Me.txtMsg.Text = ""
       "
       "btnReset
       "
       Me.btnReset.Location = New System.Drawing.Point(328, 8)
       Me.btnReset.Name = "btnReset"
       Me.btnReset.TabIndex = 2
       Me.btnReset.Text = "Reset"
       "
       "Label1
       "
       Me.Label1.Location = New System.Drawing.Point(320, 56)
       Me.Label1.Name = "Label1"
       Me.Label1.Size = New System.Drawing.Size(40, 16)
       Me.Label1.TabIndex = 3
       Me.Label1.Text = "Upper:"
       "
       "Label2
       "
       Me.Label2.Location = New System.Drawing.Point(320, 104)
       Me.Label2.Name = "Label2"
       Me.Label2.Size = New System.Drawing.Size(40, 16)
       Me.Label2.TabIndex = 4
       Me.Label2.Text = "Lower:"
       "
       "lblUpper
       "
       Me.lblUpper.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
       Me.lblUpper.Location = New System.Drawing.Point(368, 56)
       Me.lblUpper.Name = "lblUpper"
       Me.lblUpper.Size = New System.Drawing.Size(32, 23)
       Me.lblUpper.TabIndex = 5
       "
       "lblLower
       "
       Me.lblLower.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
       Me.lblLower.Location = New System.Drawing.Point(368, 104)
       Me.lblLower.Name = "lblLower"
       Me.lblLower.Size = New System.Drawing.Size(32, 23)
       Me.lblLower.TabIndex = 6
       "
       "Form1
       "
       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
       Me.ClientSize = New System.Drawing.Size(417, 293)
       Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.lblLower, Me.lblUpper, Me.Label2, Me.Label1, Me.btnReset, Me.txtMsg, Me.txtInput})
       Me.Name = "Form1"
       Me.Text = "Key Event Demonstrator"
       Me.ResumeLayout(False)
   End Sub
  1. End Region
   Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
       txtMsg.Text = ""
       txtInput.Text = ""
       lblUpper.Text = ""
       lblLower.Text = ""
   End Sub
   Private Sub txtInput_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtInput.KeyPress
       Dim keyChar As Char
       keyChar = e.KeyChar
       Console.WriteLine("KeyPress event.")
       Console.WriteLine("KeyChar: " + keyChar)
       Console.WriteLine("KeyChar Code: " + AscW(keyChar).ToString())
       txtMsg.AppendText("Handled: " + e.Handled.ToString())
       "  Fill in the Upper and Lower labels
       lblUpper.Text = keyChar.ToString().ToUpper()
       lblLower.Text = keyChar.ToString().ToLower()
       "  Change $ to #
       If (keyChar.ToString() = "$") Then
           txtInput.AppendText("#")
           e.Handled = True
       End If
   End Sub
   Private Sub KeyMsgBox(ByVal str As String, ByVal e As KeyEventArgs)
       Console.WriteLine(str + " event.")
       Console.WriteLine("KeyCode name: " + e.KeyCode.ToString())
       Console.WriteLine("KeyCode key code: " + CInt(e.KeyCode).ToString())
       Console.WriteLine("KeyData name: " + e.KeyData.ToString())
       Console.WriteLine("KeyData key code: " + CInt(e.KeyData).ToString())
       Console.WriteLine("KeyValue: " + e.KeyValue.ToString() )
       Console.WriteLine("Handled: " + e.Handled.ToString())
   End Sub
   Private Sub txtInput_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtInput.KeyDown
       KeyMsgBox("KeyDown", e)
   End Sub
   Private Sub txtInput_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtInput.KeyUp
       KeyMsgBox("KeyUp", e)
   End Sub

End Class</source>

Shift Key

<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class KeyCodeRangeShift

  public Shared Sub Main
       Application.Run(New Form1)
  End Sub

End class Public Class Form1

   Inherits System.Windows.Forms.Form
  1. 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 Label1 As System.Windows.Forms.Label
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
       Me.Label1 = New System.Windows.Forms.Label
       Me.SuspendLayout()
       "
       "Label1
       "
       Me.Label1.Location = New System.Drawing.Point(56, 80)
       Me.Label1.Name = "Label1"
       Me.Label1.Size = New System.Drawing.Size(184, 200)
       Me.Label1.TabIndex = 0
       "
       "Form1
       "
       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
       Me.ClientSize = New System.Drawing.Size(292, 273)
       Me.Controls.Add(Me.Label1)
       Me.Name = "Form1"
       Me.Text = "Form1"
       Me.ResumeLayout(False)
   End Sub
  1. End Region
   Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
       If e.KeyCode >= Keys.A And e.KeyCode <= Keys.Z Then
           If e.Shift Then
               Label1.Text &= Chr(e.KeyCode)
           Else
               Label1.Text &= Char.ToLower(Chr(e.KeyCode))
           End If
       End If
   End Sub
   Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
       Label1.Text &= e.KeyChar
   End Sub

End Class</source>