I want to make my program check whats being pasted in to my textbox and remove all disallowed characters, for example, I need to to remove all characters except numbers and commas. What would be the best way to carry this out?
Printable View
I want to make my program check whats being pasted in to my textbox and remove all disallowed characters, for example, I need to to remove all characters except numbers and commas. What would be the best way to carry this out?
use the Isnumeric function in the textbox change event
Theres two problems when using that, 1) I'd like to also have commas and 2) I'd like it to remove all non numeric characters automatically. :confused: :confused: :confused:
commas dont falsify the isnumeric test. try this:Quote:
Originally posted by Xerpher
Theres two problems when using that, 1) I'd like to also have commas and 2) I'd like it to remove all non numeric characters automatically. :confused: :confused: :confused:
VB Code:
Private Sub Text1_Change() If Not IsNumeric(Text1.Text) Then Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1) End If End Sub
Maybe trap for keystrokes. Detect if a numeric character is pressed and cancel it.
Well its a good thing that commas are allowed, but i cant allow periods... I guess I'll have to use a loop to go through each character pasted and detect whether its a number/comma or not then decide if it goes through :( So much work for such a late hour :o
try this:Quote:
Originally posted by Xerpher
Well its a good thing that commas are allowed, but i cant allow periods... I guess I'll have to use a loop to go through each character pasted and detect whether its a number/comma or not then decide if it goes through :( So much work for such a late hour :o
VB Code:
Private Sub Text1_Change() If Not IsNumeric(Text1.Text) Then Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1) End If Text1.Text = Replace(Text1.Text, ".", "") End Sub
The replace! thats it! I dont know why I didn't think of that before, I'll make a for each loop that replaces all disallowed characters stored in an array with nothing!
Thanks for the inspriration Muddy :D