Private Sub txtDesc_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtDesc.TextChanged
          Select Case Asc(e.KeyChar)
            Case 8, 32, 65 To 90, 97 To 122
                e.Handled = False
            Case Else
                e.Handled = True
        End Select
    End Sub

**I am encountering these errors:
'Handled' is not a member of 'System.EventArgs'
'KeyChar' is not a member of 'System.EventArgs'

::please help me asap :( i don't know what's wrong with these codes.**

Recommended Answers

All 5 Replies

Handled and KeyChar are only part of Key events, like KeyPress. You'll have to use one of those events to trap which keys are being pressed. If you have to use the TextChanged event you could read the Text property and isolate the last character

I get what you mean, it just I don't know how to relate it in my code. The code that I have stated above :'( please help me :(

that won't work systemeventargs can't be converted to keyeventargs.
Try this:

         Private Sub txtDesc_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtDesc.TextChanged
            Dim KeyPressed As Char
            KeyPressed = txtDesc.Text.Substring(txtDesc.Text.Length - 1).Single     
            Select Case Asc(KeyPressed)
                Case 8, 32, 65 To 90, 97 To 122
                    txtDesc.txt = txtDesc.Text.Substring(0,txtDesc.Text.Length - 2)
                Case Else
            End Select
         End Sub

This also works:

     Private Sub txtDesc_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtDesc.KeyPress
        Select Case Asc(e.KeyChar)
            Case 8, 32, 65 To 90, 97 To 122
                e.Handled = False
            Case Else
                e.Handled = True
        End Select
    End Sub

Had a brain wave lol realized there's an even cleaner way:

    Public Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        e.Handled = Char.IsNumber(e.KeyChar)   
    End Sub

numbers are handled everything else isn't.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.