Deleting letters but keeping numbers in text box

Let say that you have a text box with some numbers and you input a letter by accident... and I want to keep the numbers there and just delete the letter/s

i've got this but that deletes everything that is in the box

If Not IsNumeric(txt_length.Text) Then
txt_length.Text = ""
End If

any ideas?

Comments

  • Restricting the input to numbers only can be done in the KeyPress event. Use Select Case to check the value of KeyAscii. If KeyAscii associates with a numeric key, do nothing, otherwise change the value of KeyAscii to 0.

    One thing a lot of people miss when using this technique is to allow both the number keys and keys on the numkeypad to go through, as well as navigation keys (Home, End, Backspace, Del, etc.) Just remember to think about every key that might be necessary and not just the visible result.

    Here's the basics to get you started:

    [code]
    Private Sub Text1_KeyPress(KeyAscii As Integer)
    Select Case KeyAscii
    Case vbKey0 To vbKey9, vbKeyNumpad0 To vbKeyNumpad9
    'These will correspond to the numbers
    Case vbKeyHome, vbKeyEnd, vbKeyBack, vbKeyLeft, vbKeyRight, vbKeyDelete
    'These are basic navigation within the textbox
    Case vbKeyReturn, vbKeyTab
    'Navigation throughout the controls on the form
    Case Else
    KeyAscii = 0
    End Select
    End Sub
    [/code]

  • You made some respectable factors there. I regarded on the internet for the difficulty and located most people will go along with along with your website.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion