I am sure the answer is out there, but I am unsure how to word it...
So I am looking for an event or a way , if the user has clicked on textbox1 (and put their input in) they can press enter and it will do a task. But if they did that with textbox2, then it would do a different task.
Sorry for the wording, cannot think of a better way to explain
Code currently trying. But all it is doing is making the 'ding' sound on enter.
Private Sub StoreNumberPT_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles StoreNumberPT.KeyDown
If e.KeyCode = Keys.Enter Then
If Trim(StoreNumberPT.Text) <> vbNullString Then
MsgBox("success")
Else
MsgBox("success")
End If
End If
End Sub
Private Sub textbox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles textbox1.KeyDown
If e.KeyCode = Keys.Enter Then
If Trim(textbox1.Text) <> vbNullString Then
' code
Else
'code
End If
End If
End Sub
or you can handle keydown event for each TextBox under one method like below
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles _
TextBox1.KeyDown, TextBox2.KeyDown
Dim txtbx = DirectCast(sender, TextBox)
If e.KeyCode = Keys.Enter And Trim(txtbx.Text) <> vbNullString Then
Select Case txtbx.Name
Case "TextBox1"
'your code when user type in TextBox1, as a sample
MsgBox(txtbx.Text)
Case "TextBox2"
'your code when user type in TextBox2
MsgBox(txtbx.Text)
End Select
End If
End Sub
use
Private Sub textbox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles textbox1.KeyDown,textbox2.KeyDown
If e.KeyCode = Keys.Enter Then
If sender.name="Textbox1"
If Trim(textbox1.Text) <> vbNullString Then
' code
Else
'code
End If
Elseif sender.name="Textbox2"
If Trim(textbox2.Text) <> vbNullString Then
' code
Else
'code
End If
End if
End if
End Sub
Hope this helps.
Related
The code says everything, if I press F1 and I'm already with Button1 selected, then it should select Button2 and vice-versa.
If keyData = Keys.F1 Then
If Button1.Select() = True Then
Button2.Select()
ElseIf Button2.Select() = True Then
Button1.Select()
Else
Button1.Select()
End If
End If
But the expression "Button2.Select() = True" doesn't return a value.
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If e.KeyValue = Keys.F1 Then
If ActiveControl.Name = "Button1" Then
Button2.Select()
Else
Button1.Select()
End If
End If
End Sub
Actually, F1 is a poor choice because it is traditionally the "Help" key.
We can use form keydown (set form keypreview property to true) or use button previewkeydown like these:
Private Sub Button1_PreviewKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles Button1.PreviewKeyDown
If e.KeyCode = Keys.F1 Then
Button2.Focus()
End If
End Sub
Private Sub Button2_PreviewKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles Button2.PreviewKeyDown
If e.KeyCode = Keys.F1 Then
Button1.Focus()
End If
End Sub
I'm currently writing code for a project in VB.NET where if you hit the "escape" button you get a messagebox saying "This exits the program, would you like to leave? 'Y' or 'N'". What code would I write to make it so that if you hit "Y" the program closes?
This is what I have so far:
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Escape Then
MsgBox("This button exits the program. Do you want to exit the program?")
If Button = e.KeyCode = Keys.Y Then
End
End If
End If
End Sub
This will give you what you are after:
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Escape Then
If MessageBox.Show("message", "caption", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
Close()
End If
End If
End Sub
Screenshot showing how it looks when I press the Esc button:
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
Select Case e.KeyCode
Case Keys.Escape
Msgbox("Close",vbInformation)
Me.Close()
End Select
End Sub
Hope This Helps as a guide.
Try This One
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If e.KeyCode = Keys.Escape Then
Dim ask As MsgBoxResult
ask = MsgBox("Close Program ?", MsgBoxStyle.YesNo, "Exit")
If ask = MsgBoxResult.Yes Then
Me.Close()
End If
End If
End Sub
This is a small piece of code for a project I'm working on for my study.
We have to make a program in Visual Basic (with Visual Studio 2015) that copies text from the first text box and pastes it into the second one once you press the button that states: "Show Name".
We are meant to handle it so that if the value entered is between 'A' to 'Z' then it copies the text and pastes it normally once you press the button into the second text box.
We are also supposed to make it so that, if the value is a number (between 0 and 9) we are meant to have a message which pops up saying something like: "Error - You Entered a Number".
We are also supposed to make a box that pops up saying: "Error - You Entered Something Other Than a Number", if the value is a character other than a letter or a number. I am in kind of a rush and would appreciate any help soon.
Here is my code so far. (I know Keys.A and Keys.Z and the message box is wrong, which I need to fix, too):
Public Class MyFirstProgram
Private Sub DisplayTextButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DisplayTextButton.Click
ShowTextBox.Text = EnterTextBox.Text
End Sub
Private Sub me_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
Dim letterEntered As Char
If e.KeyCode < Keys.A Or e.KeyCode > Keys.Z Then
MsgBox("Error - Use letter keys only!", MsgBoxStyle.OkOnly, )
Else
letterEntered = LCase(ChrW(e.KeyCode))
If ShowTextBox.Text = "" Then
ShowTextBox.Text = letterEntered
Else
ShowTextBox.Text = ShowTextBox.Text + letterEntered
End If
End If
End Sub
Private Sub ClearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearButton.Click
EnterTextBox.Text = ""
End Sub
Private Sub MyFirstProgram_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.KeyPreview = True
End Sub
End Class
Oh, and I'm new to Visual Basic/programming; so, please try to be patient if you can. Sorry :/
Use KeyPress press event .. instead of keyDown
Private Sub me_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
'If e.KeyCode < Keys.A Or e.KeyCode > Keys.Z Then
' MsgBox("Error - Use letter keys only!", MsgBoxStyle.OkOnly, )
'Else
' Dim letterEntered = LCase(ChrW(e.KeyCode))
' ShowTextBox.Text = ShowTextBox.Text + letterEntered
'End If
'e.Handled = True
End Sub
Private Sub Form1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Me.KeyPress
If Not Char.IsLetter(e.KeyChar) Then
MsgBox("Error - Use letter keys only!", MsgBoxStyle.OkOnly)
e.Handled = True
End If
End Sub
just look in to the IsNumeric Function if it could help you.
I have enabled the KeyPreview option on the form and on the keydown of the form I am detecting the Escape keydown to popup a msgbox requesting whether the user wants to exit from the application.
But the problem is, I have one textbox that needs to clear its contents on Escape keypress rather than asking whether the user needs to exit. And I tried using the keydown event on this control but the form keydown event occurs before the control keydown (I want this to happen the other way around).
Code on the Textbox
Private Sub txtAmount(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtAmount.KeyDown
If e.KeyCode = Keys.Escape Then
'//Escape keypress
MsgBox("TEXTBOX ESCAPE")
sender.Text = ""
End If
End Sub
Code used on the Main menu Form
Private Sub frmMainMenu_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Escape Then Application.Exit()
End Sub
I can't use the previewkeydown either because the MaskTextBox doesn't raise the previewkeydown event..
Check if txtAmount is focused in the Form_KeyDown:
Private Sub txtAmount_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles txtAmount.KeyDown
If e.KeyCode = Keys.Escape Then
'//Escape keypress
MsgBox("TEXTBOX ESCAPE")
sender.Text = ""
End If
End Sub
Private Sub frmMainMenu_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If txtAmount.Focused Then Return
If e.KeyCode = Keys.Escape Then Application.Exit()
End Sub
Edit: Alternatively, if you don't want to specify each control individually, you could do it by type:
If TypeOf Me.ActiveControl Is MaskTextBox Then Return
Or even more simpler, change your Form_KeyDown-Event to a KeyUp Event.
Public Class frmMainMenu
Private KeyHandled As Boolean
Private Sub txtAmount_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles txtAmount.KeyDown
If e.KeyCode = Keys.Escape Then
sender.Text = ""
KeyHandled = True
End If
End Sub
Private Sub frmMainMenu_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
KeyHandled = False
End Sub
Private Sub frmMainMenu_KeyUp(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
If KeyHandled Then Return
If e.KeyCode = Keys.Escape Then Application.Exit()
End Sub
End Class
Here is my code
Private Sub When_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown, MyBase.KeyDown
If sender.name = "Your form name" Then
If e.KeyCode = Keys.Escape And Not TextBox1.Focused Then
MsgBox("me")
End If
ElseIf sender.name = "your textBox name" Then
If e.KeyCode = Keys.Escape Then
MsgBox("txt")
End If
End If
End Sub
I hope this can help you
I am trying to capture TAB keypress on Keydown Event.
I can see another post on How to fire an event when the tab key is pressed in a textbox?
However, On the above link, posted solution is not working for me which I mentioned below.
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) _
Handles TextBox1.KeyDown
If e.KeyCode = Keys.Tab Then
e.SuppressKeyPress = True
'do something
End If
End Sub
For the testing purpose, I have added 2 simple textboxes on FORM1 and write the below code to capture the TAB on KeyDown event.
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Tab Then
e.SuppressKeyPress = True
MsgBox("TAB DOWN")
End If
End Sub
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Me.Text = e.KeyChar
End Sub
Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
If e.KeyCode = Keys.Tab Then
MsgBox("TAB UP")
End If
End Sub
Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
Me.Text = "LEAVE"
End Sub
My above code should suppose to display a message box on KeyDown when TAB is press. It's not working.
Please let me know what I am doing wrong.
Thanks in advance!!!
I found a new event called PreviewKeyDown()
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Tab Then
Me.Text = "TAB Capture From TextBox1_KeyDown At " & Now.ToString
End If
End Sub
Private Sub TextBox1_PreviewKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles TextBox1.PreviewKeyDown
If e.KeyCode = Keys.Tab Then
Me.Text = "TAB Capture From TextBox1_PreviewKeyDown At " & Now.ToString
End If
End Sub
If you will execute the above code, you will able to capture TAB key on PreviewKeyDown() event.
MsgBox() is a holdover from VB6 and you should use the .NET implementation of a message box, like this:
MessageBox.Show("TAB UP")
Also, you are setting a Text property against the instance of the form class (Me), when I think you intend to set the Text property of the text box, like this:
Me.TextBox1.Text = e.KeyChar