making multiply keypress event shorter vb.net - vb.net

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

Related

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

keypress on datagridview only character "abcde" and convert to uppercase

How to limit keypress on datagridview only character abcde and convert to uppercase at vb.net?
Private Sub DataGridView1_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
If DataGridView1.CurrentCell.ColumnIndex = 3 Then
AddHandler CType(e.Control, TextBox).KeyPress, AddressOf TextBoxabcde_keyPress
End If
End Sub
Private Sub TextBoxabcde_keyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
If Not Char.IsControl(e.KeyChar) And Not Char.IsLetter(e.KeyChar) And e.KeyChar <> "." Then
e.Handled = True
End If
End Sub
Updated:
Private Sub DataGridView1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles DataGridView1.KeyPress
Dim allLetters As String = "abcde"
If Not allLetters.Contains(e.KeyChar.ToString.ToLower) Then
e.KeyChar = ChrW(0)
e.Handled = True
End If
End Sub
Private Sub dataGridView1_CellFormatting(sender As Object, e As
DataGridViewCellFormattingEventArgs)
If e.Value IsNot Nothing Then
e.Value = e.Value.ToString().ToUpper()
e.FormattingApplied = True
End If
End Sub
another solution,im try with this, its work
Private Sub DataGridView1_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
If DataGridView1.CurrentCell.ColumnIndex = 3 Then
DirectCast(e.Control, TextBox).CharacterCasing = CharacterCasing.Upper
AddHandler CType(e.Control, TextBox).KeyPress, AddressOf TextBoxabcde_keyPress
End If
End Sub
Private Sub TextBoxabcde_keyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
If Not (Asc(e.KeyChar) = 8) Then
Dim allowedChars As String = "ABCDE"
If Not allowedChars.Contains(e.KeyChar.ToString.ToUpper) Then
e.Handled = True
End If
End If
End Sub
thanks for your help Claudius
Try this
Dim txtEC As DataGridViewTextBoxEditingControl = Nothing
Private Sub DGV_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DGV.EditingControlShowing
If TypeOf e.Control Is DataGridViewTextBoxEditingControl Then
If DGV.CurrentCell.ColumnIndex =1 Then
txtEC = DirectCast(e.Control, DataGridViewTextBoxEditingControl)
txtEC.CharacterCasing = CharacterCasing.Upper
End If
End If
End Sub
Private Sub DGV_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles DGV.CellEndEdit
If txtEC IsNot Nothing Then
txtEC.CharacterCasing = CharacterCasing.Normal
txtEC = Nothing
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

How to put a semicolon separator in a textbox in textchange event?

hi guys another question from a noob i manage to separate the number type in a textbox with a semicolon but the problem is when using backspace the semicolon cannot be deleted..
my code is this
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
If TextBox1.TextLength Mod 3 = 2 Then
SendKeys.SendWait(":")
End If
End Sub
Slight fudge, and maybe a more elegant solution but...
Dim ValidKey As Boolean = True
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
If Char.IsLetterOrDigit(e.KeyChar) Then
ValidKey = True
Else
ValidKey = False
End If
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
If ValidKey And TextBox1.TextLength Mod 3 = 2 Then
SendKeys.SendWait(":")
End If
End Sub
You could of course expand on this by vetting keys for only digits if you wanted....
EDIT: You can minimise this code to;
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
If Char.IsLetterOrDigit(e.KeyChar) And TextBox1.TextLength Mod 3 = 2 Then
SendKeys.SendWait(":")
End If
End Sub
Removing the TextChanged Sub, the superfluous variable and else statement...
Edit 2: For restricting to numerical input only...
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
If Char.IsDigit(e.KeyChar) And TextBox1.TextLength Mod 3 = 2 Then
SendKeys.SendWait(":")
ElseIf Char.IsLetter(e.KeyChar) Then
e.Handled = True
End If
End Sub
Try this:
Dim backPressed As Boolean = False
Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
If backPressed Then
backPressed = False
Return
End If
If TextBox1.TextLength Mod 3 = 2 Then
SendKeys.SendWait(":")
End If
End Sub
Private Sub TextBox1_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar = ChrW(8) Then
backPressed = True
End If
End Sub

Simulate header click in DataGridView

I'm trying to simulate a headerclick in datagridview column 1 with shortcut keys but I dont know how.
Private Sub frm_lista_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.S And e.Alt Then
'this handler exists
DataGridView1_ColumnHeaderMouseClick(whathere, whathere)
End If
End Sub
Any help?
Make sure the form has this property set:
Me.KeyPreview = True
Since you are trying to specify column 1 from a keyboard event, you can pass your own event arguments:
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
If ((Control.ModifierKeys And e.Alt) AndAlso e.KeyCode = Keys.S) Then
Dim mea As New MouseEventArgs(MouseButtons.None, -1, -1, -1, -1)
Dim dgvcme As New DataGridViewCellMouseEventArgs(1, -1, -1, -1, mea)
DataGridView1_ColumnHeaderMouseClick(DataGridView1, dgvcme)
End If
End Sub
The event should now have the e.ColumnIndex property set.
Private Sub DataGridView1_ColumnHeaderMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DataGridView1.ColumnHeaderMouseClick
If e.ColumnIndex = 1 Then
MessageBox.Show("OK")
End If
End Sub
Alternatively, you could just call a sub-routine from both places:
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
If ((Control.ModifierKeys And e.Alt) AndAlso e.KeyCode = Keys.S) Then
Call HandelColumnOneClick()
End If
End Sub
Private Sub DataGridView1_ColumnHeaderMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DataGridView1.ColumnHeaderMouseClick
If e.ColumnIndex = 1 Then
Call HandleColumnOneClick()
End If
End Sub
Private Sub HandleColumnOneClick()
'// Do Something
End Sub