Key event summary : Key Event « Event « VB.Net Tutorial






Key event summary
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

#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 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

#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








11.6.Key Event
11.6.1.Check Key Code: F5Check Key Code: F5
11.6.2.Displaying information about a user-pressed keyDisplaying information about a user-pressed key
11.6.3.Determine whether user pressed Enter keyDetermine whether user pressed Enter key
11.6.4.Ignore key codeIgnore key code
11.6.5.Key event summaryKey event summary
11.6.6.Control KeyControl Key
11.6.7.Shift KeyShift Key
11.6.8.Check Key Code rangeCheck Key Code range