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?
Related
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
The following code eats the Enter key for a TextBox (with "MultiLine" set to True):
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
If Asc(e.KeyChar) = 13 Then
e.Handled = True
End If
End Sub
The same code for a RichTextBox however doesn't work: The Enter key is not eaten:
Private Sub RichTextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles RichTextBox1.KeyPress
If Asc(e.KeyChar) = 13 Then
e.Handled = True
End If
End Sub
I don't see where I could have made a mistake.
Does anybody see where this behaviour might come from?
Got it.
I needs to be consumed in KeyDown. Unlike for TextBox:
Private Sub RichTextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles RichTextBox1.KeyDown
If e.KeyCode = Keys.Enter Then
e.Handled = True
End If
End Sub
So I've been working on this project to autorun on my flash drive, whenever it gets plugged it. It just displays my contact info and a little message, so I can get it returned.
Because I want people to actually read the message, the close button is disabled. You have to check a little box, and then hit the "OK" button.
Of course, I don't want to do this whenever I plug in my own flash drive. I'm trying to make a keyboard shortcut to close it automatically. I saw this post here but it didn't work for me. Here's the code from that post:
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
If e.Alt AndAlso e.KeyCode = Keys.X Then
Application.Exit()
End If
End Sub
Any help is very appreciated. Thanks!
Also, if you think my code is sloppy and want to clean it up, here it is.
Public Class Form1
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
e.Cancel = True
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles ButtonOK.Click
End
End Sub
Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.Checked = True Then
ButtonOK.Enabled = True
Else
ButtonOK.Enabled = False
End If
End Sub
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
End Sub
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
If e.Alt AndAlso e.KeyCode = Keys.X Then
End
End If
End Sub
End Class
Firstly, NEVER EVER use End. That is just plain bad under any circumstances.
What you should be doing is testing the Checked value of your CheckBox in the FormClosing event handler and cancelling if and only if it's not checked. Logically, you should then check the CheckBox and call Close on the form when you detect that desired key combination.
Actually, I'd probably not call Close either, but rather call PerformClick on the Button. That way, the key combination is guaranteed to do exactly the same thing as clicking the Button.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles ButtonOK.Click
Close()
End Sub
Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
ButtonOK.Enabled = CheckBox1.Checked
End Sub
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
If e.Alt AndAlso e.KeyCode = Keys.X Then
CheckBox1.Checked = True
ButtonOK.PerformClick()
End If
End Sub
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
e.Cancel = Not CheckBox1.Checked
End Sub
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
I am trying to make a super basic program that involves a picture and sound popping up every time I click F4. I have the background of the program set to green, because I am going to be using it as a green screen for the picture. I don't have much experience with VB, but since I couldn't find a program to do this on the web, I decided to take a swing and try to make it myself. (Failed...) Anyways, this is what I got so far.
Public Class Form1
Private Sub Form1_KeyPress(KeyAscii As Integer)
If (Chr(KeyAscii) = "115") Then Form1.Picture = loadpicture("directory")
End Sub
End Class
Note: "Directory" is not what I have in loadpicture().
Try this:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.KeyPreview = True 'This enable the key event on the form (me).
End Sub
Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
If e.KeyCode = Keys.F4 Then Me.BackgroundImage = Image.FromFile("C:\image.jpg")
End Sub
This is the final code that also includes an audio clip that plays when the key is pressed as well!
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.KeyPreview = True 'This enable the key event on the form (me).
End Sub
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.F4 Then Me.BackgroundImage = Image.FromFile("C:\image.jpg")
If e.KeyCode = Keys.F4 Then My.Computer.Audio.Play("C:\audio.wav", AudioPlayMode.Background)
End Sub
Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
If e.KeyCode = Keys.F4 Then Me.BackgroundImage = Nothing
End Sub
End Class