简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM