none
Using Enter Key RRS feed

  • Question

  • How do you use the enter key in a program so that if it is pressed something happens like submitting something without a button
    Monday, July 1, 2013 4:15 PM

Answers

  • Hi,

     If you don`t want a button to be used then you can use the forms KeyDown event to detect if it was the enter key pressed and then activate whatever code, function, or sub you want. You will need to set the forms (KeyPreview) property to True for it to work.

    Public Class Form1
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.KeyPreview = True
        End Sub
    
        Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
            If e.KeyCode = Keys.Enter Then
                MessageBox.Show("Run your sub, function, or other code")
            End If
        End Sub
    End Class


    EDIT : Please edit and change this thread to a question instead of a Discussion.  :)
    • Edited by IronRazerz Monday, July 1, 2013 4:30 PM edit
    • Proposed as answer by LeonCS Tuesday, July 2, 2013 6:46 AM
    • Marked as answer by Youen ZenModerator Wednesday, July 17, 2013 3:35 PM
    Monday, July 1, 2013 4:26 PM
  • In addition to what Reed has suggested, if you do not have any buttons on your form at all, you can simply process the keystroke and take action depending on which key was pressed.

    example:

    Option Strict On
    Public Class Form1
        Private Sub ProcessForm()
            'Your Form Code here
            MsgBox("Enter Pressed!")
        End Sub
        Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown, TextBox2.KeyDown, TextBox3.KeyDown
            If e.KeyCode = Keys.Enter Then
                ProcessForm()
                e.Handled = True
                e.SuppressKeyPress = True
            End If
        End Sub
    End Class
    


    “If you want something you've never had, you need to do something you've never done.”

    Don't forget to mark helpful posts and answers ! Answer an interesting question? Write a new article about it! My Articles
    *This post does not reflect the opinion of Microsoft, or its employees.

    Monday, July 1, 2013 4:33 PM
    Moderator

All replies

  • How do I set that?
    Monday, July 1, 2013 4:24 PM
  • Hi,

     If you don`t want a button to be used then you can use the forms KeyDown event to detect if it was the enter key pressed and then activate whatever code, function, or sub you want. You will need to set the forms (KeyPreview) property to True for it to work.

    Public Class Form1
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.KeyPreview = True
        End Sub
    
        Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
            If e.KeyCode = Keys.Enter Then
                MessageBox.Show("Run your sub, function, or other code")
            End If
        End Sub
    End Class


    EDIT : Please edit and change this thread to a question instead of a Discussion.  :)
    • Edited by IronRazerz Monday, July 1, 2013 4:30 PM edit
    • Proposed as answer by LeonCS Tuesday, July 2, 2013 6:46 AM
    • Marked as answer by Youen ZenModerator Wednesday, July 17, 2013 3:35 PM
    Monday, July 1, 2013 4:26 PM
  • I thought I set it as a question obviously not. your answer was the best.
    Monday, July 1, 2013 4:32 PM
  • In addition to what Reed has suggested, if you do not have any buttons on your form at all, you can simply process the keystroke and take action depending on which key was pressed.

    example:

    Option Strict On
    Public Class Form1
        Private Sub ProcessForm()
            'Your Form Code here
            MsgBox("Enter Pressed!")
        End Sub
        Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown, TextBox2.KeyDown, TextBox3.KeyDown
            If e.KeyCode = Keys.Enter Then
                ProcessForm()
                e.Handled = True
                e.SuppressKeyPress = True
            End If
        End Sub
    End Class
    


    “If you want something you've never had, you need to do something you've never done.”

    Don't forget to mark helpful posts and answers ! Answer an interesting question? Write a new article about it! My Articles
    *This post does not reflect the opinion of Microsoft, or its employees.

    Monday, July 1, 2013 4:33 PM
    Moderator
  • On a Webform program, An XAML program, A phone program or maybe even a kind of old type of User Interface?

    The user interface you use makes that there are many solutions for this.

    Be aware Windows has never used the VT100 terminal where the Enter key did exist as a kind of Return key.


    Success
    Cor

    Monday, July 1, 2013 4:36 PM
  • Cor, quit showing off your computer. lol  :)
    Monday, July 1, 2013 4:39 PM
  • On a Webform program, An XAML program, A phone program or maybe even a kind of old type of User Interface?

    The user interface you use makes that there are many solutions for this.

    Be aware Windows has never used the VT100 terminal where the Enter key did exist as a kind of Return key.


    Success
    Cor

    LOL


    You've taught me everything I know but not everything you know. _________________________________________________________________________________________________________________ This search engine is for MSDN Library and has many features. http://social.msdn.microsoft.com/Search/en-US?query=search%20msdn%20library&beta=0&ac=8

    Monday, July 1, 2013 6:26 PM
  • Return keys have not changed at all.

    I has two VT100s on my desk at Landover.

    But cor, you've missed the obvious. The vt100 was an ascii terminal. Todays terminals are graphics displays,

    Renee


    "MODERN PROGRAMMING is deficient in elementary ways BECAUSE of problems INTRODUCED by MODERN PROGRAMMING." Me

    Monday, July 1, 2013 6:32 PM
  • How do I set that?

    Well if you read the provided link it tells you to set the Forms property accordingly.


    You've taught me everything I know but not everything you know. _________________________________________________________________________________________________________________ This search engine is for MSDN Library and has many features. http://social.msdn.microsoft.com/Search/en-US?query=search%20msdn%20library&beta=0&ac=8

    Monday, July 1, 2013 8:56 PM
  • The following two examples for when you want to process the enter key to a specific control type.

    Something else to include, data entry people tend to like pressing the enter key to move from control to control i.e textbox to textbox etc.

    Private Sub _Load(
        sender As Object,
        e As EventArgs) _
    Handles MyBase.Load
        For Each tb In (From T In Me.Controls.OfType(Of TextBox)()).ToList
            AddHandler tb.KeyDown, AddressOf TextBox1_KeyDown
        Next
    End Sub
    Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs)
        If e.KeyCode = Keys.Enter Then
            e.SuppressKeyPress = True
            SelectNextControl(ActiveControl, True, True, True, True)
        End If
    End Sub

    Similarly we might want to handle enter key for a DataGridView separately from other controls

    Public Class frmKeyEnterDemo
        Private EditRow As Integer = -1
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            DataGridView1.ColumnCount = 3
            DataGridView1.Columns(0).Name = "First Name"
            DataGridView1.Columns(0).DataPropertyName = "Object"
            DataGridView1.Columns(1).Name = "Last Name"
            DataGridView1.Columns(2).Name = "Occupation"
            DataGridView1.Rows.Add(New Object() {"Rod", "Stephens", "Nerd"})
            DataGridView1.Rows.Add(New Object() {"Sergio", "Aragones", "Cartoonist"})
            DataGridView1.Rows.Add(New Object() {"Eoin", "Colfer", "Author"})
            DataGridView1.Rows.Add(New Object() {"Terry", "Pratchett", "?"})
        End Sub
        Private Sub DataGridView1_EditingControlShowing(
            ByVal sender As Object,
            ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) _
        Handles DataGridView1.EditingControlShowing
            EditRow = DataGridView1.CurrentRow.Index
        End Sub
        Private Sub DataGridView1_SelectionChanged(
            ByVal sender As Object,
            ByVal e As System.EventArgs) _
        Handles DataGridView1.SelectionChanged
            If EditRow >= 0 Then
                Dim new_row As Integer = EditRow
                EditRow = -1
                DataGridView1.CurrentCell =
                    DataGridView1.Rows(new_row).Cells(DataGridView1.CurrentCell.ColumnIndex)
            End If
        End Sub
        Private Sub DataGridView1_KeyDown(
            ByVal sender As Object,
            ByVal e As System.Windows.Forms.KeyEventArgs) _
        Handles DataGridView1.KeyDown
            If e.KeyCode = Keys.Return Then
                If DataGridView1.CurrentRow.Index < DataGridView1.RowCount - 1 AndAlso
                    DataGridView1.CurrentCell.ColumnIndex = DataGridView1.ColumnCount - 1 Then
                    DataGridView1.CurrentCell = DataGridView1(0, DataGridView1.CurrentRow.Index + 1)
                Else
                    Dim CurrentCell As DataGridViewCell = DataGridView1.CurrentCell
                    Dim col As Integer = CurrentCell.ColumnIndex
                    col = (col + 1) Mod DataGridView1.Columns.Count
                    CurrentCell = DataGridView1.CurrentRow.Cells(col)
                    DataGridView1.CurrentCell = CurrentCell
                End If
                e.Handled = True
            End If
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Tuesday, July 2, 2013 12:46 AM
    Moderator
  • Put the action code in a button_click event

    set the property value "Visible" of the button to "False"

    Set the "AcceptButton" property of the Form to the action button.

    Tuesday, July 2, 2013 8:45 AM