Click here to Skip to main content
14,766,371 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello Guys,
I m'looking for someone who may help concerning how to restrict textbox whether by letters, numbers or specials characters.

I got some cods that used to restrict Text box bellow:

Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        If Not Char.IsLetterOrDigit(e.KeyChar) Then
            e.Handled = True
        End If
    End Sub

they are working with keyPress but not working with text-change, so I need that work with text change that can provide a message when wrong input is entered. I continued doing my research I reached to the following code, but I did not know how I can change them so that they can work for numbers and also how they can work with special character, please help me out. the codes were these bellow
Public Class MainForm

    Dim charactersDisallowed As String = "1234567890"

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        Dim theText As String = TextBox1.Text
        Dim Letter As String

        For x As Integer = 0 To TextBox1.Text.Length - 1
            Letter = TextBox1.Text.Substring(x, 1)
            If charactersDisallowed.Contains(Letter) Then
                theText = theText.Replace(Letter, String.Empty)
            End If

        TextBox1.Text = theText
        TextBox1.Select(TextBox1.Text.Length, 0)
    End Sub

End Class

I want to know how I can apply them for numbers allowance and special characters allowance.
Thanks7872 12-Sep-13 4:55am
Do you have access to google? Why you are posting non questions frequently? We are not here to do homework for you.

1 solution

DAVID DUSHIMIMANA 12-Sep-13 5:18am
Thank u Rakesh Meel

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900