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

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

Related

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 suppress sound when opening a dialogform in vb.net

I have a form with a textbox, when pressing enter another form2 is opened.
How can I suppress sound when form2 is opened?
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Enter Then
e.Handled = True
e.SuppressKeyPress = True
My.Forms.Form2.ShowDialog()
End If
End Sub
You could do this by changing the code slightly and moving it into the KeyPress event instead of KeyDown
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyKeyPress
If e.KeyChar = ChrW(Keys.Enter) Then
e.Handled = True
My.Forms.Form2.ShowDialog()
End If
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

Eat key in KeyDown

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

focus on the next text box if KeyCode=.(DOT)

I have 4 textboxes which are used to store IP Addresses. I want to automate the selection of the textboxes by inputting the .(DOT) character; I want also this character to be deleted right after having been input. I have been able to get the auto-selection functionality, but not the character deletion part. Here is my code:
If e.KeyCode = Keys.OemPeriod Then
TextBox2.Focus()
End If
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If Asc(e.KeyChar) = 46 Then 'It does not matter how you select the character
TextBox2.Focus()
e.Handled = True 'To avoid the character to be written (i.e., delete the DOT)
End If
End Sub
Private Sub TextBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress
If Asc(e.KeyChar) = 46 Then
TextBox3.Focus()
e.Handled = True
End If
End Sub
Private Sub TextBox3_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox3.KeyPress
If Asc(e.KeyChar) = 46 Then
TextBox4.Focus()
e.Handled = True
End If
End Sub