Determine whether user pressed Enter key : Key Event « Event « VB.Net Tutorial






Determine whether user pressed Enter key
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








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