This question already has answers here:
Multiple event handlers for the same event in VB.NET
(3 answers)
Closed 7 years ago.
can anyone help me simplify this code, because I need to extend the same way in much more line, this make my code look bulky
Private Sub tb11_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles tb11.KeyPress
If e.KeyChar >= ("1") And e.KeyChar <= ("9") Then
tb11.Text = e.KeyChar
End If
End Sub
Private Sub tb12_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles tb12.KeyPress
If e.KeyChar >= ("1") And e.KeyChar <= ("9") Then
tb12.Text = e.KeyChar
End If
End Sub
Private Sub tb13_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles tb13.KeyPress
If e.KeyChar >= ("1") And e.KeyChar <= ("9") Then
tb13.Text = e.KeyChar
End If
End Sub
Private Sub tb14_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles tb14.KeyPress
If e.KeyChar >= ("1") And e.KeyChar <= ("9") Then
tb14.Text = e.KeyChar
End If
End Sub
Make a single event handler that runs the check then changes the sender text. Remember to cast the sender to a TextBox first though.
Private Sub TextBox_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs)
Handles tb11.KeyPress, tb12.KeyPress, tb13.KeyPress, tb14.KeyPress
If e.KeyChar >= ("1") And e.KeyChar <= ("9") Then
DirectCast(sender, TextBox).Text = e.KeyChar
End If
End Sub
Above all the event listeners functions you are checking the same condition
you can move that condition to a other method and pass your object to it
Related
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
I have a lot of textboxes on my form (around 70). I want them to accept only HEX value. I have to write KeyPress event for each of the textboxes manually, and it is little bit frustrating. Is it possible to make this shorter?
Private Sub TextBox66_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox66.KeyPress
If Not "1234567890ABCDEF".Contains(Char.ToUpper(e.KeyChar)) AndAlso e.KeyChar <> vbBack Then
e.Handled = True
End If
End Sub
Private Sub TextBox65_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox65.KeyPress
If Not "1234567890ABCDEF".Contains(Char.ToUpper(e.KeyChar)) AndAlso e.KeyChar <> vbBack Then
e.Handled = True
End If
End Sub
Private Sub TextBox64_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox64.KeyPress
If Not "1234567890ABCDEF".Contains(Char.ToUpper(e.KeyChar)) AndAlso e.KeyChar <> vbBack Then
e.Handled = True
End If
End Sub
Private Sub TextBox63_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox63.KeyPress
If Not "1234567890ABCDEF".Contains(Char.ToUpper(e.KeyChar)) AndAlso e.KeyChar <> vbBack Then
e.Handled = True
End If
End Sub
Private Sub TextBox62_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox62.KeyPress
If Not "1234567890ABCDEF".Contains(Char.ToUpper(e.KeyChar)) AndAlso e.KeyChar <> vbBack Then
e.Handled = True
End If
End Sub
Private Sub TextBox61_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox61.KeyPress
If Not "1234567890ABCDEF".Contains(Char.ToUpper(e.KeyChar)) AndAlso e.KeyChar <> vbBack Then
e.Handled = True
End If
End Sub
Private Sub TextBox52_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox52.KeyPress
If Not "12345678".Contains(Char.ToUpper(e.KeyChar)) AndAlso e.KeyChar <> vbBack Then
e.Handled = True
End If
End Sub
Private Sub TextBox60_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox60.KeyPress
If Not "1234567890ABCDEF".Contains(Char.ToUpper(e.KeyChar)) AndAlso e.KeyChar <> vbBack Then
e.Handled = True
End If
End Sub
Private Sub TextBox59_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox59.KeyPress
If Not "1234567890ABCDEF".Contains(Char.ToUpper(e.KeyChar)) AndAlso e.KeyChar <> vbBack Then
e.Handled = True
End If
End Sub
Try this:
Create the eventhandler once from the form load event. This way you do not create redundant code.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each textbox As TextBox In Me.Controls.OfType(Of TextBox)
If textbox.Name.StartsWith('TextHex') Then
AddHandler textbox.KeyPress, AddressOf OnTextBoxKeyPress
End If
Next
End Sub
This is called for every keypress on your textbox
Private Sub OnTextBoxKeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs)
If Not "1234567890ABCDEF".Contains(Char.ToUpper(e.KeyChar)) AndAlso e.KeyChar <> vbBack Then
e.Handled = True
End If
End Sub
Finally, do a cleanup by removing the eventhandlers we defined during form load.
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
For Each textbox As TextBox In Me.Controls.OfType(Of TextBox)
If textbox.Name.StartsWith('TextHex') Then
RemoveHandler textbox.KeyPress, AddressOf OnTextBoxKeyPress
End If
Next
End Sub
If your textboxes are inside another control (groupbox, panel), then you should change the scope used in the for loop from Me.Controls to (name of groupbox/panel).Controls
You can also list as many TextBoxes as you like after the "Handles" keyword; just separate them by commas like this:
Private Sub TextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox52.KeyPress, TextBox59.KeyPress, TextBox60.KeyPress, TextBox61.KeyPress, TextBox62.KeyPress, TextBox63.KeyPress, TextBox64.KeyPress, TextBox65.KeyPress,TextBox66.KeyPress
If Not "1234567890ABCDEF".Contains(Char.ToUpper(e.KeyChar)) AndAlso e.KeyChar <> vbBack Then
e.Handled = True
End If
End Sub
If you ever need the source Textbox, cast the "sender" parameter to a local variable of type TextBox:
Private Sub TextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox52.KeyPress, TextBox59.KeyPress, TextBox60.KeyPress, TextBox61.KeyPress, TextBox62.KeyPress, TextBox63.KeyPress, TextBox64.KeyPress, TextBox65.KeyPress,TextBox66.KeyPress
Dim tb As TextBox = DirectCast(sender, TextBox)
If Not "1234567890ABCDEF".Contains(Char.ToUpper(e.KeyChar)) AndAlso e.KeyChar <> vbBack Then
e.Handled = True
End If
End Sub
I'm trying to block my text boxes in order that they can only accept numbers. I was looking in the Internet and I found this code
Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) HandlesTextBox1.TextChanged
If Not Char.IsNumber(e.KeyChar) And Not Char.IsControl(e.KeyChar) Then
e.Handled = True
End If
End Sub
The thing is that there is an error message displayed:
e.keychar is not a member of system.eventargs
I already imported the Imports System.EventArgs. Any idea how to fix this?
I believe the problem lies in the event you’re handling. It should probably not be TextChanged. e.KeyChar is usually in the EventArgs for KeyPress handlers. Try using something like TextBox1_KeyPress.
This should work:
Public Class Form1
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs)
If e.KeyValue < 48 OrElse e.KeyValue > 57 Then _
e.SuppressKeyPress = True
End Sub
End Class
It needs to have (sender As Object, e As KeyPressEventArgs)
which e.KeyChar is a member of
I would like to know how i can validate a textbox to not allow any decimal values?
this solution i got from this link ( How to allow user to enter only numbers in a textbox in vb.net? )
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If (Microsoft.VisualBasic.Asc(e.KeyChar) < 48) _
Or (Microsoft.VisualBasic.Asc(e.KeyChar) > 57) Then
e.Handled = True
End If
If (Microsoft.VisualBasic.Asc(e.KeyChar) = 8) Then
e.Handled = False
End If
End Sub
You can use the KeyPress event and use the IsNumeric Function to trap the numeric keys.
Private Sub TextBox1_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If IsNumeric(e.KeyChar) Then
e.Handled = True
End If
End Sub
If you can, use a MaskedTextBox
Since handling the KeyPress can cause problem with delete/backspace/copy/paste/...
I'm creating a simple application in Vb.net where I need to perform certain validations. So I want a name textbox to accept only characters from a-z and A-Z for example.
For this I wrote the following code:
Private Sub txtname_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox5.KeyPress
If Asc(e.KeyChar) <> 8 Then
If Asc(e.KeyChar) > 65 Or Asc(e.KeyChar) < 90 Or Asc(e.KeyChar) > 96 Or Asc(e.KeyChar) < 122 Then
e.Handled = True
End If
End If
End Sub
But somehow it is not allowing me to enter characters. When I try to enter any character it does nothing.
What causes this problem and how can I resolve it?
Alternatively, you can do something like this,
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) _
Handles txtName.KeyPress
If Not (Asc(e.KeyChar) = 8) Then
Dim allowedChars As String = "abcdefghijklmnopqrstuvwxyz"
If Not allowedChars.Contains(e.KeyChar.ToString.ToLower) Then
e.KeyChar = ChrW(0)
e.Handled = True
End If
End If
End Sub
or if you still want the ASCII way, try this,
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtName.KeyPress
If Not (Asc(e.KeyChar) = 8) Then
If Not ((Asc(e.KeyChar) >= 97 And Asc(e.KeyChar) <= 122) Or (Asc(e.KeyChar) >= 65 And Asc(e.KeyChar) <= 90)) Then
e.KeyChar = ChrW(0)
e.Handled = True
End If
End If
End Sub
both will behave the same.
This is a more abstract approach, but still effective nonetheless. It's straightforward and can just be added to the TextChanged event. You can use it with multiple textboxes by adding their handles to the sub and using a DirectCast().
Dim allowed As String = ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
For Each c As Char In TextBox.Text
If allowed.Contains(c) = False Then
TextBox.Text = TextBox.Text.Remove(TextBox.SelectionStart - 1, 1)
TextBox.Select(TextBox.Text.Count, 0)
End If
Next
Summary: If an invalid character is entered, it will immediately be deleted (most of the time the character won't be visible long enough for the user to notice).
Private Sub txtname_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox5.KeyPress
If AscW(e.KeyChar) > 64 And AscW(e.KeyChar) < 91 Or AscW(e.KeyChar) > 96 And AscW(e.KeyChar) < 123 Or AscW(e.KeyChar) = 8 Then
Else
e.KeyChar = Nothing
End If
End Sub
I hope this helps!
Am using below code to accept only letters, "-" or "'" when user is entering a person name (maybe it is simple and easier):
Private Sub txtEName1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtEName1.KeyPress
If Char.IsLetter(e.KeyChar) Or e.KeyChar = vbBack Or e.KeyChar = "-" Or e.KeyChar = "'" Then
e.Handled = False
Else
e.Handled = True
End If
End Sub
I allowed the backspace in order to allow the user to remove letters in case of mistakes
Private Sub txtStudentName_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtStudentName.KeyPress
If Not Char.IsLetter(e.KeyChar) And Not e.KeyChar = Chr(Keys.Delete) And Not e.KeyChar = Chr(Keys.Back) And Not e.KeyChar = Chr(Keys.Space) Then
e.Handled = True
End If
End Sub