How to avoid special characters in textbox VB.net

Hi, do you have any shortcut for avoiding special characters in textbox?
im using this manual code:

Public Class Form1
    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
        If TextBox1.ToString.Contains("`") Then
            MsgBox("Text contains invalid character(s)", vbInformation, "Invalid!")
        ElseIf TextBox1.ToString.Contains("~") Then
            MsgBox("Text contains invalid character(s)", vbInformation, "Invalid!")
        ElseIf TextBox1.ToString.Contains("!") Then
            MsgBox("Text contains invalid character(s)", vbInformation, "Invalid!")
        ElseIf TextBox1.ToString.Contains("@") Then
            MsgBox("Text contains invalid character(s)", vbInformation, "Invalid!")
        ElseIf TextBox1.ToString.Contains("#") Then
            MsgBox("Text contains invalid character(s)", vbInformation, "Invalid!")
        ElseIf TextBox1.ToString.Contains("$") Then
            MsgBox("Text contains invalid character(s)", vbInformation, "Invalid!")
        ElseIf TextBox1.ToString.Contains("%") Then
            MsgBox("Text contains invalid character(s)", vbInformation, "Invalid!")
        ElseIf TextBox1.ToString.Contains("^") Then
            MsgBox("Text contains invalid character(s)", vbInformation, "Invalid!")
        ElseIf TextBox1.ToString.Contains("*") Then
            MsgBox("Text contains invalid character(s)", vbInformation, "Invalid!")
        ElseIf TextBox1.ToString.Contains("&") Then
            MsgBox("Text contains invalid character(s)", vbInformation, "Invalid!")
        ElseIf TextBox1.ToString.Contains("(") Then
            MsgBox("Text contains invalid character(s)", vbInformation, "Invalid!")
        ElseIf TextBox1.ToString.Contains(")") Then
            MsgBox("Text contains invalid character(s)", vbInformation, "Invalid!")
        ElseIf TextBox1.ToString.Contains("_") Then
            MsgBox("Text contains invalid character(s)", vbInformation, "Invalid!")
        ElseIf TextBox1.ToString.Contains("-") Then
            MsgBox("Text contains invalid character(s)", vbInformation, "Invalid!")
        ElseIf TextBox1.ToString.Contains("=") Then
            MsgBox("Text contains invalid character(s)", vbInformation, "Invalid!")
        ElseIf TextBox1.ToString.Contains("+") Then
            MsgBox("Text contains invalid character(s)", vbInformation, "Invalid!")
        ElseIf TextBox1.ToString.Contains(";") Then
            MsgBox("Text contains invalid character(s)", vbInformation, "Invalid!")
        ElseIf TextBox1.ToString.Contains("\") Then
            MsgBox("Text contains invalid character(s)", vbInformation, "Invalid!")
        ElseIf TextBox1.ToString.Contains("|") Then
            MsgBox("Text contains invalid character(s)", vbInformation, "Invalid!")
        ElseIf TextBox1.ToString.Contains("/") Then
            MsgBox("Text contains invalid character(s)", vbInformation, "Invalid!")
        ElseIf TextBox1.ToString.Contains("[") Then
            MsgBox("Text contains invalid character(s)", vbInformation, "Invalid!")
        ElseIf TextBox1.ToString.Contains("]") Then
            MsgBox("Text contains invalid character(s)", vbInformation, "Invalid!")
        ElseIf TextBox1.ToString.Contains("{") Then
            MsgBox("Text contains invalid character(s)", vbInformation, "Invalid!")
        ElseIf TextBox1.ToString.Contains("}") Then
            MsgBox("Text contains invalid character(s)", vbInformation, "Invalid!")
        End If
    End Sub
End Class

Open in new window


Thanks
Rowel VirgoVisual Studio .NETAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

 
NorieVBA ExpertCommented:
Do you want to remove the invalid characters?

Perhaps something like this?
    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
        Dim c As Char
        Dim InvalidCharacters As String = "`~!@#$%^*&()_-=+;\|/[]{}"
        Dim pos As Long = 0

        c = TextBox1.Text.Substring(TextBox1.Text.Length - 1, 1)
        pos = InvalidCharacters.IndexOf(c)
        If pos <> -1 Then
            TextBox1.Text = TextBox1.Text.Remove(TextBox1.Text.Length - 1)
            TextBox1.SelectionStart = TextBox1.Text.Length
        End If

    End Sub

Open in new window

1
 
Rowel VirgoVisual Studio .NETAuthor Commented:
Wow nice sir thank you
0

Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
 
NorieVBA ExpertCommented:
Rowel

Why are you closing the question without acknowledging if a solution was given?
0
 
Rowel VirgoVisual Studio .NETAuthor Commented:
Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.