Search code examples
vb.netwinformstextboxkeypress

making multiply keypress event shorter 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

Solution

  • 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