Press Enter in TextBox to Trigger Click Event in VB.NET

This is a very simple tutorial of mine yet a very useful one. This is a program that when you press the enter key in the textbox, it will trigger to have the click event of the button using the VB.NET programming language. Now, let's start this tutorial! 1. Let's start with creating a Windows Form Application for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Windows Application. 2. Next, add only one Button named Button1 and a textbox named TextBox1 on the Form. 3. Now, we will do the coding! First, we will put a code statement on the Click event of our Button. We will just put a messagebox there.
  1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  2. MsgBox("Thank you for clicking!")
  3. End Sub
Now, for pressing the enter key of the textbox we will use the Keypress Event of the TextBox.
  1. Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
  2. If e.KeyChar = Chr(13) Then 'Chr(13) is the Enter Key
  3. 'Runs the Button1_Click Event
  4. Button1_Click(Me, EventArgs.Empty)
  5. End If
  6. End Sub
The e.KeyChar emphasizes to get the key character that was pressed by the user and the chr(13) indicates the Enter Key. So meaning if pressing the Enter key using this code will trigger to run the Button's Click Event. The 'Me' as part of the Button1_Click parameters indicates that the clicking will be in the current form.

Output:

output For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Comments

Submitted byramjay (not verified)on Tue, 09/21/2021 - 10:36

Thank you

Add new comment