Results 1 to 16 of 16

Thread: Restrict TextBox to only certain characters, numeric or symbolic

Threaded View

  1. #1

    Thread Starter
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    Restrict TextBox to only certain characters, numeric or symbolic

    The following are some coding examples on how to customize which characters you want to keep from being entered into a TextBox.

    To specify which characters are the only ones that are allowed to be in the TextBox:

    (In this example only letters and numbers will be accepted into the textbox)
    Code:
    Public Class MainForm
    
        Dim charactersAllowed As String = " abcdefghijklmnopqrstuvwxyz1234567890"
    
        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
            Dim SelectionIndex As Integer = TextBox1.SelectionStart
            Dim Change As Integer
    
            For x As Integer = 0 To TextBox1.Text.Length - 1
                Letter = TextBox1.Text.Substring(x, 1)
                If charactersAllowed.Contains(Letter) = False Then
                    theText = theText.Replace(Letter, String.Empty)
                    Change = 1
                End If
            Next
    
            TextBox1.Text = theText
            TextBox1.Select(SelectionIndex - Change, 0)
        End Sub
    
    End Class

    To specify which characters can not be entered into the textbox:

    (In this example any numbers will not be accepted into the textbox)
    Code:
    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
            Dim SelectionIndex As Integer = TextBox1.SelectionStart
            Dim Change As Integer
    
            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)
                    Change = 1
                End If
            Next
    
            TextBox1.Text = theText
            TextBox1.Select(SelectionIndex - Change, 0)
        End Sub
    
    End Class
    The above codes will also prevent copying and pasting from bringing unwanted characters.
    Last edited by Vectris; Jun 2nd, 2009 at 05:46 PM.
    If your problem is solved, click the Thread Tools button at the top and mark your topic as Resolved!

    If someone helped you out, click the button on their post and leave them a comment to let them know they did a good job

    __________________
    My Vb.Net CodeBank Submissions:
    Microsoft Calculator Clone
    Custom TextBox Restrictions
    Get the Text inbetween HTML Tags (or two words)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width