Eat key in KeyDown - vb.net

Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.V Then
If e.Control = True Then
e.Handled = True 'eat it
End If
End If
End Sub
This does not work.
Does anybody know why and how to achieve the same in a proper way?
Thank you!
ps: Why is there a .Handled property that is writable, but it does not do anything? I guess I missed something.

You should capture the KeyPress event instead of KeyDown:
Private isCopyPaste As Boolean = False
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
isCopyPaste = (e.KeyCode = Keys.V AndAlso e.Control)
End Sub
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs ) Handles TextBox1.KeyPress
If isCopyPaste Then
e.Handled = True 'eat it
End If
End Sub
Also check this reference on MSDN: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keypress.aspx

Use the SuppressKeyPress instead:
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.V Then
If e.Control = True Then
e.SuppressKeyPress = True
End If
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?

e.Handled in RichTextBox has no effect. On TextBox, it works

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

Windows form hotkeys

I'm trying to use hotkeys on a form to hide/show a textbox, tried many ways and after reading this thread, I did this:
If Control.ModifierKeys = Keys.B Then
If TextBox1.Visible = True Then
TextBox1.Visible = False
Else
TextBox1.Visible = True
End If
and:
If Control.ModifierKeys = Keys.B Then
If TextBox1.Visible = True Then
TextBox1.Hide()
Else
TextBox1.Hide()
End If
End If
but still not working.. help me, please
First you need to set the forms property
KeyPreview = True
Then use the forms KeyDown event
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.B Then TextBox1.Visible = Not TextBox1.Visible
End Sub
If you want to use an ALT or other combination you need to check the modifiers too
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If CBool(e.Modifiers And Keys.Alt) AndAlso e.KeyCode = Keys.B Then TextBox1.Visible = Not TextBox1.Visible
End Sub
If you want to do lots of them then use a select case statement
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
Select Case e.KeyCode
case Keys.B : TextBox1.Visible = Not TextBox1.Visible
'etc
End Select
End Sub

Disabling Alt+F4 function in key down event

How to prevent form being closed by alt+f4 keys in keydown event? problem i am facing is keeping ALT key pressed and pressing F4 closes form but i want to prevent form from closing. Below is my code
Private Sub frminstituteselect_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If e.Alt = True And e.KeyCode = Keys.F4 Then
e.Handled = True
End If
End Sub
Try like this
Private ALT_F4 As Boolean = False
Private Sub frminstituteselect_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Form1.FormClosing
If ALT_F4 Then
e.Cancel = True
Return
End If
End Sub
Private Sub frminstituteselect_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Form1.KeyDown
ALT_F4 = (e.KeyCode.Equals(Keys.F4) AndAlso e.Alt = True)
End Sub
or simply
Private Sub frminstituteselect_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Form1.FormClosing
e.Cancel = True
End Sub