Text Filtering in VB

Creating a text filter isn’t as easy as it sounds. At least, in Visual Basic its not.

I spent several hours figuring out how to bypass all the little errors I get when I try to remove text from a Textbox.

Well, anyone else who needs to know how, here you go.

[code]
For i As Integer = 0 To TextBox1.TextLength - 1
Try
If TextBox1.Text.Chars(i) = “.” Or TextBox1.Text.Chars(i) = “*” _
Or TextBox1.Text.Chars(i) = “=” Or TextBox1.Text.Chars(i) = “,” Or _
TextBox1.Text.Chars(i) = “!” Or TextBox1.Text.Chars(i) = “_” Or TextBox1.Text.Chars(i) = “/” _
Or TextBox1.Text.Chars(i) = “(” Or TextBox1.Text.Chars(i) = “)” Or TextBox1.Text.Chars(i) = “’” _
Or TextBox1.Text.Chars(i) = “” Or TextBox1.Text.Chars(i) = “&” Or _
TextBox1.Text.Chars(i) = “-” Then

                TextBox1.Text = TextBox1.Text.Replace(TextBox1.Text.Chars(i), "")


            End If

        Catch ex As Exception

        End Try


    Next[/code]

Remember, you can always change the code to filter other characters.

Thanks,

Other options might include some of the string functions like the function IndexOfAny.

I have not tried it, but you might be able to do something like create a character array, and then use IndexOfAny to search your string for an occurance of your characters. If the index is -1 then there are none else you can replace that character.

The other way I have done it in the past is to use the instr function, in much the way I mentioned above.

Kurt

You’re welcome kurt. :slight_smile:

check out regular expressions.