Thank you for being a valued part of the CNET community. As of December 1, 2020, the forums are in read-only format. In early 2021, CNET Forums will no longer be available. We are grateful for the participation and advice you have provided to one another over the years.

Thanks,

CNET Support

General discussion

Automatic Backspace

Oct 24, 2008 11:24AM PDT

Hi, I'm playing with Visual Basic, and I'm trying to do an automatic backspace on a text box once I enter a non numeric character, but I haven't been able to do so. The only thing than I have come to is clear everything, when just is one character the problem.

This is the code:

Private Sub Textbox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Textbox1.TextChanged
If Not IsNumeric(Textbox1.Text) Then
Textbox1.Clear()
End If
End Sub

Any help would be grateful, except google ones. Thanks in advance.

Discussion is locked

- Collapse -
So why not?
Oct 24, 2008 11:41AM PDT

Something like...

Textbox1.text = Left$(Textbox1.text, len(Textbox1.text)-1)

Sorry, I leave correcting the syntax to you. Plus the code only fires when you enter text so no trouble with 1 char long.
Bob

- Collapse -
Thanks
Oct 25, 2008 1:44AM PDT

Thanks a lot.

I only had to ad Microsoft.VisualBasic. to Left

But the cursor, for some reason, moved to the beginning of the Textbox, so I had to:

Textbox1.SelectionStart = Textbox1.TextLength

If there's another way, in which I don't have to use the second code, please, let me know, if not, thanks anyway.