How to Set Enter is ok on Textbox in vb.net - vb.net

I have a password textbox (Tbx1) and a Login button (Btn1). What code will i put where so that when the user
inputs password and presses enter, Btn1 clickevents will be exucuted?

Try this:
Private Sub TextBox1_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Enter Then
fun()
End If
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
fun()
End Sub
Private Sub fun()
MessageBox.Show("Enter key pressed")
End Sub

on Form Misc Properties set the accept button to be Btn1. Equally, you can set a cancel button too to be executed whenever the user presses Esc button.
Code version (through the constructor):
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Me.AcceptButton = Btn1
End Sub

Related

Specific keyboard characters to call specific button actions

When I start my program, and push the specified keys to call my button commands, it does not do anything.
The focus remains on one of the buttons and nothing occurs.
I've tried multiple codes from using KeyDown to KeyPress to codes that include vbKey.
I am very new to vb, so it is very likely that I just do understand what I am doing. :(
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Me.KeyPreview = True
End Sub
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
If e.KeyCode = "/" Then
Call Button1_Click(sender, e)
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnExit.Click
'Close Program
Me.Close()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPMN.Click
'Add +1 to PMN Textbox (txtPMN)
txtPMN.Text = (Val(txtPMN.Text) + 1).ToString()
End Sub
End Class
I would like to simulate the clicking of certain buttons (activate the button code) when I press specific keys on the keyboard. For example: If I press "/" I would like Button1_Click to activate as if I clicked the button with the mouse. Then, if I push my next key ".", I would like Button2_Click to activate.
I do not want to use modifiers like: SHIFT, CTRL, ALT
Please try with keychar like this:
Private Sub Form1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Me.KeyPress
If e.KeyChar = "/" Then
Call Button1_Click(sender, New EventArgs)
End If
end sub
when use keydown, the keycode for "/" key is OEM, depend the keyboard type, so char(e.keycode) may be not "/"

VB.NET simulate button click and how do I return it to normal?

I have difficulty with this code, I explain:
I have created a textbox and when I press enter, it simulates clicking on a button.
Sub TextBox1KeyDown(sender As Object, e As KeyEventArgs)
If e.KeyCode = Keys.Enter Then
'simulate to press Button
Button1Click(sender,New EventArgs)
Else
'code to return to normal
End If
End Sub
It works fine, but how do I return it to normal? I mean, when I click the button, it does not react.
Please wait for your help with this problem that arises
It is usually not a good idea to call an event procedure. Move your code from the Button1.Click to a separate sub then call it from both places.
Private Sub TextBox1_KeyDown_1(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Enter Then
'simulate to press Button
MyButtonSub()
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
MyButtonSub()
End Sub
Private Sub MyButtonSub()
MessageBox.Show("Button One was clicked or pretend clicked.")
End Sub

Order of KeyEvents processing in Winforms with VB.NET

I have subclassed form with 3 controls-NumericUpDown in it.
Here is main form' code:
Public Class Form1
Inherits cls_subform
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.KeyPreview = True
End Sub
Private Sub NumericUpDown1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles NumericUpDown1.KeyDown
If e.KeyCode = Keys.Enter Then
Debug.Print "Control's ENTER is pressed"
End If
End Sub
End Class
Here is simplified frm_subform code:
Public Class cls_subform
Inherits System.Windows.Forms.Form
Public Sub New()
InitializeComponent()
End Sub
Protected Overrides Sub onKeyDown(ByVal e As System.Windows.Forms.KeyEventArgs)
If e.KeyCode = Keys.Enter Then
e.Handled = True
e.SuppressKeyPress = True
Debug.Print "Subclassed ENTER is pressed"
End If
MyBase.OnKeyDown(e)
End Sub
End Class
This obviously work propper.
But problem is that I would like to handle control's event handler NumericUpDown1_KeyDown FIRST. And then if it is not "handled" I would like to handle onKeyDown event handler on frm_subform.
If I set KeyPreview to False then I can catch control's event but then onKeyDown is not fired.
Is there a way to get desired functionality?
That FIRST will be fired control's _KeyDown handler and then form's onKeyDown handler?

Disable sounds when pressed enter key to append text on richtextbox

I have this in my design form.
My codes are the follwoing
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
RichTextBox1.AppendText(TextBox1.Text)
End Sub
Private Sub TextBox1_Keyup(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyUp
If e.KeyCode = Keys.Enter Then
Call Button2_Click(sender, e)
End If
End Sub
The problem is when I click on textbox, typed something in it then pressed enter I will hear a beeping sound. That sound is the one that I want to disable.
I also notice that if I just typed in the textbox and click the button I can hear no sound, the sound occurs whenever I clicked on textbox, type something in it and pressed enter.
EDIT:
By conducting a thorough research, I realized that the ding sounds doesn't come from button pressed but from the last line of richtextbox. It is the same sound that we can heard when we pressed key_down and we are in the last line of a richtextbox. How do I disable it?
Just set the AcceptButton() property of the Form to Button2. Then you don't need the KeyUp code at all:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.AcceptButton = Button2
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
RichTextBox1.AppendText(TextBox1.Text)
End Sub
' Don't need this code anymore:
'Private Sub TextBox1_Keyup(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyUp
' If e.KeyCode = Keys.Enter Then
' Call Button2_Click(sender, e)
' End If
'End Sub

How to submit on keypress (vb.net)

I have written a login. Now I want to do this (press return to login):
Private Sub login_KeyDown(ByVal sender As Object,
ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyData = Keys.Return Then
login()
End If
End Sub
It does not work. At this time it works only with a button.
Try using the KeyDown events of the textboxes instead.
Protected WithEvents txtUsername As TextBox
Protected WithEvents txtPassword As TextBox
Private Sub Login_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtUsername.KeyDown, txtPassword.KeyDown
If e.KeyData = Keys.Return Then login()
End Sub
Or you could just set the AcceptButton property of your form to be the button you would like to be "pressed" when return/enter is pressed. This is exactly what this feature is here for.