Key Event Handler in VB.NET
In this article we will learn how to use Key Event handler.
Determine whether user pressed Enter key
1.Open visual studio and create a project in
vb.net
2.Use the following code in form1.vb
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
#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.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
#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
OUTPUT
OF THE APPLICATION
