textbox.focus does not work. vb - vb.net

i have a problem with command textbox.focus it does not work "when I entered the key it goes to under line inside the textbox so i can not make another command because there is a one space or the navigation arrow still in the second row inside the textbox"...
this is my code
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Enter Then
TextBox1.Clear()
TextBox1.Focus()
End If
End Sub
when i use this code
If e.KeyCode = Keys.Enter Then
msgbox("any thing")
TextBox1.Clear()
TextBox1.Focus()
End If
it works good
any one help me please?
thanks

I have got the answer
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Enter Then
TextBox1.Text = ""
SendKeys.Send("{BACKSPACE}")
TextBox1.Focus()
End If
End Sub

Related

VB disable "|" key

I'm trying to disable the | key in a textbox as my csv file uses the character as a separator. However, I tried many methods and searched on google but I can't find any solutions related to that.
My code:
Private Sub txtRemarks_KeyDown(sender As Object, e As KeyEventArgs) Handles txtRemarks.KeyDown
If e.KeyCode = Keys.Shift And e.KeyCode = AscW(220) Then
e.SuppressKeyPress = True
End If
End Sub
Wonder if that is possible? Thanks
If you want to detect a key in combination with one or more modifiers then there are multiple ways to do it. You could do this:
Private Sub txtRemarks_KeyDown(sender As Object, e As KeyEventArgs) Handles txtRemarks.KeyDown
If Not e.Control AndAlso e.Shift AndAlso Not e.Alt AndAlso e.KeyCode = Keys.Oem5 Then
e.SuppressKeyPress = True
End If
End Sub
or this:
Private Sub txtRemarks_KeyDown(sender As Object, e As KeyEventArgs) Handles txtRemarks.KeyDown
If e.Modifiers = Keys.Shift AndAlso e.KeyCode = Keys.Oem5 Then
e.SuppressKeyPress = True
End If
End Sub
but my preference would be this:
Private Sub txtRemarks_KeyDown(sender As Object, e As KeyEventArgs) Handles txtRemarks.KeyDown
If e.KeyData = (Keys.Shift Or Keys.Oem5) Then
e.SuppressKeyPress = True
End If
End Sub
Note that that third snippet uses KeyData rather than KeyCode. The latter is just the current key while the former is the current key and any modifier keys.
If you want to make it a little more succinct:
Private Sub txtRemarks_KeyDown(sender As Object, e As KeyEventArgs) Handles txtRemarks.KeyDown
e.SuppressKeyPress = (e.KeyData = (Keys.Shift Or Keys.Oem5))
End Sub

How do you stop this annoying ding?

I know the question has been asked and answered many times, but what if none of the answers work? I'm using Visual Studio 2017 to rewrite a vb6 app that catches the key combination "ctrl+enter" to load a dialogue.
I catch the key combination just fine from a textbox called CourtName, but can't get rid of the annoying "ding" that goes with it.
I have googled for many hours but everywhere the answer is to use e.Handled and/or e.SuppressKeyPress, which I have done without success.
Here is my code:
Private Sub CourtName_KeyDown(sender As Object, e As KeyEventArgs) Handles CourtName.KeyDown
If e.KeyCode = Keys.Enter AndAlso e.Control Then
e.Handled = True
e.SuppressKeyPress = True
CourtsBtn.PerformClick()
End If
End Sub
The ding still persists, no matter whether the e.Handled and e.SuppressKeyPress statements are before or after the PerformClick() statement.
What magic am I missing?
Try replacing the following code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TextBox1.Multiline = True
MsgBox("Pressed!")
End Sub
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyData = Keys.Control + Keys.Enter Then
TextBox1.Multiline = False
Button1.PerformClick()
End If
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1.Multiline = True
End Sub
I've tested this code on mine, it works perfect!
Enjoy it!
I just tested this code and there was no ding, no matter what modifiers were used or not with Enter:
Public Class Form1
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Enter Then
'Suppress Windows audio feedback.
e.Handled = True
e.SuppressKeyPress = True
End If
If e.KeyData = (Keys.Control Or Keys.Enter) Then
Button1.PerformClick()
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Console.WriteLine("Button1_Click")
End Sub
End Class
Can you test the same code and see whether it works for you?

How to make keypress (keydown) close program

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

Keypress in not read in visual basic 2010/2012

My app is based on Visual Basic 2010/2012 (all its codes are same for both the languages). I'm trying to do certain things when either the 'F' or 'G' is pressed from keyboard. I've tried all the things like using Keypress, keydown and keyup events but they all didn't work.
Here is a sample code of my app. This just shows which key was pressed by the user in a message box and this also doesn't work i.e. nothing happens, not even an error message.
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If (e.KeyCode = Keys.Control AndAlso (e.KeyCode = Keys.F)) Then
MessageBox.Show("pressed F")
ElseIf (e.KeyCode = Keys.Control AndAlso (e.KeyCode = Keys.B)) Then
MessageBox.Show("pressed B")
End If
End Sub`
My take on optimizing the KeyDown handler, using DRY principle (=don't repeat yourself):
Private Sub Form1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If Not e.Control Then Exit Sub
Select Case e.KeyCode
Case Keys.F : MessageBox.Show("Ctrl-F")
Case Keys.B : MessageBox.Show("Ctrl-B")
End Select
End Sub
In keypressed
Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
dim kc as string = e.KeyChar
if kc = "F" OR kc = "G" then msgbox "Horeeee"
End Sub
In addition to setting KeyPreview() to True for your Form, as already pointed out by #Tony Hopkinson, here is how to correctly check for Ctrl-F or Ctrl-B:
Private Sub Form1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.Control AndAlso e.KeyCode = Keys.F Then
MessageBox.Show("Ctrl-F")
ElseIf e.Control AndAlso e.KeyCode = Keys.B Then
MessageBox.Show("Ctrl-B")
End If
End Sub

Keydown event weird behaviour

I'm trying to catch shortcut keys. I need an explanation on how KeyDown Events are managed. Let's take this as an example :
Private Sub SoldeOuvertFou_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If e.Control And e.KeyCode = Keys.W Then
MessageBox.Show("Ctrl+W")
End If
If e.Control And e.KeyCode = Keys.F5 Then
MessageBox.Show("Ctrl+F5")
End If
End Sub
Works perfectly. No matter which one I press first or how many times I press them both MessageBox will pop up. Now if I simply change the order inside the sub :
Private Sub SoldeOuvertFou_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If e.Control And e.KeyCode = Keys.F5 Then
MessageBox.Show("Ctrl+F5")
End If
If e.Control And e.KeyCode = Keys.W Then
MessageBox.Show("Ctrl+W")
End If
End Sub
With this approach, only Ctrl+F5 will pop-up. No way to make Ctrl+W appear... any idea why ?
you can simply use :
If e.Control Then
If e.KeyCode = Keys.F5 Then
MessageBox.Show("Ctrl+F5")
Else
If e.KeyCode = Keys.W Then
MessageBox.Show("Ctrl+W")
End If
End If
End If