PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
Restrict TextBox to only certain characters, numeric or symbolic-VBForums
Results 1 to 16 of 16

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

  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)

  2. #2
    Fanatic Member
    Join Date
    Sep 2007
    Posts
    839

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

    thanks Vectris Good example...

  3. #3
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,834

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

    Quote Originally Posted by Vectris View Post
    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
    
            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)
                End If
            Next
    
            TextBox1.Text = theText
            TextBox1.Select(TextBox1.Text.Length, 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
    
            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
            Next
    
            TextBox1.Text = theText
            TextBox1.Select(TextBox1.Text.Length, 0)
        End Sub
    
    End Class
    The above codes will also prevent copying and pasting from bringing unwanted characters.


    I think the loop can be avoid using this method

    Code:
     Dim charactersAllowed As String = " abcdefghijklmnopqrstuvwxyz1234567890"
            Dim mystr As String = "asdf"
            If mystr.IndexOfAny(charactersAllowed.ToCharArray) > 1 Then
    
            End If
    Please mark you thread resolved using the Thread Tools as shown



    www.techreceipe.tk


  4. #4
    Hyperactive Member gooden's Avatar
    Join Date
    Dec 2006
    Location
    Portugal
    Posts
    274

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

    vb Code:
    1. Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    2.         Dim theText As String = TextBox1.Text
    3.         Dim Letter As String
    4.         Dim sel_s As Integer = TextBox1.SelectionStart
    5.         Dim did_change As Boolean = False
    6.         For x As Integer = 0 To TextBox1.Text.Length - 1
    7.             Letter = TextBox1.Text.Substring(x, 1)
    8.             If charactersAllowed.Contains(Letter) = False Then
    9.                 theText = theText.Replace(Letter, String.Empty)
    10.                 did_change = True
    11.             End If
    12.         Next
    13.         TextBox1.Text = theText
    14.         If did_change = False Then
    15.             TextBox1.Select(sel_s, 0)
    16.         Else
    17.             TextBox1.Select(sel_s - 1, 0)
    18.         End If
    19.     End Sub

    I Did some changes so the selected does not change or jump.


    The Future Is Always The Way To Live The Life

  5. #5

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

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

    Quote Originally Posted by danasegarane View Post
    I think the loop can be avoid using this method

    Code:
     Dim charactersAllowed As String = " abcdefghijklmnopqrstuvwxyz1234567890"
            Dim mystr As String = "asdf"
            If mystr.IndexOfAny(charactersAllowed.ToCharArray) > 1 Then
    
            End If
    And then how are you going to get rid of any letters not in charactersAllowed?

    Look at it the other way, if you use charactersDisallowed like that. How are you going to know which character is the disallowed one? You'll either end up looping through the characters in the text or the characters in the disallowedArray.

    @gooden

    I was to lazy to add something like that at the time, thanks for the suggestion. I added selection point saving in my own way (slightly shorter, skips the if statement).
    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)

  6. #6
    Member
    Join Date
    Dec 2003
    Posts
    38

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

    Is there a way to do this in VS 2003? The function contains doesn't work.

  7. #7

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

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

    Quote Originally Posted by daved2424 View Post
    Is there a way to do this in VS 2003? The function contains doesn't work.
    Sorry for not responding to this in a timely manner but for the sake of answering the question I've decided to respond.

    I'm not exactly sure what commands VS 2003 does or doesn't support but if .Contains is the only problem check if you can use .IndexOf. If you can just replace the .Contains with .IndexOf and use = -1 for the second part of the condition instead of = False and <> -1 for True.
    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)

  8. #8
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

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

    What about this?

    Code:
    Public Class MainForm
    
        Dim charactersAllowed() As Char = " abcdefghijklmnopqrstuvwxyz1234567890".ToCharArray()
        Dim lastText As String = String.Empty
    
        Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
            If Me.TextBox1.Text.IndexOfAny(charactersAllowed) > -1 Then
                    Me.TextBox1.Text = lastText
            Else
                    lastText = Me.TextBox1.Text
            End If
        End Sub
    
    End Class

  9. #9

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

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

    I think you meant that to be for charactersDISallowed but yes that works just fine. I'm not sure if there's a way to apply that method to charactersAllowed though. If there is go ahead and post your own codebank topic to it because textbox restrictions are a common question in the vb.net forums and it will make it a lot easier if you have a quick topic you can link them to.
    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)

  10. #10
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

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

    Ah, OK.

  11. #11
    Addicted Member
    Join Date
    Mar 2010
    Location
    Southeast Michigan
    Posts
    146

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

    Just curious. Is there a reason not to use this method for allowed characters?

    Code:
    Private _allowedCharacters As String = "0123456789"
    
    Private Sub UserIDText_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles UserIDText.KeyPress
         If Not _allowedCharacters.Contains(e.KeyChar) AndAlso e.KeyChar <> ChrW(Keys.Back) Then
             e.Handled = True
         End If
     End Sub
    EDIT: Ahh, sorry just read this from the OP "The above codes will also prevent copying and pasting from bringing unwanted characters.". Where the KeyPress event won't handle it.
    Last edited by dlscott56; Jun 9th, 2010 at 10:33 AM.
    Dave

    Helpful information I've found here so far : The Definitive "Passing Data Between Forms" : Restrict TextBox to only certain characters, numeric or symbolic :
    .NET Regex Syntax (scripting) : .NET Regex Language Element : .NET Regex Class : Regular-Expressions.info
    Stuff I've learned here so far : Bing and Google are your friend. Trying to help others solve their problems is a great learning experience

  12. #12
    New Member
    Join Date
    Feb 2013
    Posts
    6

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

    Quote Originally Posted by Vectris View Post
    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 = ".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 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
    Could this be modified to allow only one instance of a character? In this case the "." so you couldn't enter a number with 2 "." if you were entering a double.

  13. #13
    New Member
    Join Date
    Jul 2014
    Posts
    1

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

    Module Main
    Public DisallowedCharacters As String = "1234567890!#$%&/()=?Ąż\/*´'~`{}^@<>,;.:_¨|°Ź+-""[]^¨"
    End Module

    Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles TextBox1.TextChanged
    If TextBox1.Text <> Empty AndAlso TextBox1.Enabled Then
    If TextBox1.Text.IndexOfAny(DisallowedCharacters.ToCharArray) >= 0 Then
    Position = TextBox1.Text.IndexOfAny(DisallowedCharacters.ToCharArray)
    If Position = 0 Then
    TextBox1.Text = Mid(TextBox1.Text, 2)
    Else
    TextBox1.Text = Mid(TextBox1.Text, 1, Position) & Mid(TextBox1.Text, Position + 2)
    End If
    TextBox1.SelectionStart = Position
    Beep()
    End If
    End If
    End Sub

  14. #14
    New Member
    Join Date
    Jun 2016
    Posts
    1

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

    I know this thread is old however I've just come across it with a Google search, so thought i'd post this if anybody else does...

    I believe the simplest way to disallow characters is to use the Textbox.KeyPress event.
    The below snippet handles disallowed characters AND maximum number of lines if you have a Multiline Textbox:

    Code:
        Private Sub txtAddress_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtAddress.KeyPress
            'Below Handles Disallowed Characters
            Dim DisallowedCharacters As String = "'~`{}^¨|°Ź+-[]^¨"
            If InStr(DisallowedCharacters, e.KeyChar) > 0 Then
                e.Handled = True
            End If
    
            'Below Handles max number of lines for multiline Textbox
            If txtAddress.Lines.Length >= 6 AndAlso e.KeyChar = ControlChars.Cr Then
                e.Handled = True
            End If
    
        End Sub
    Last edited by si_the_geek; Jun 30th, 2016 at 06:59 AM. Reason: added Code tags

  15. #15
    New Member
    Join Date
    Sep 2016
    Posts
    1

    Question Re: Restrict TextBox to only certain characters, numeric or symbolic

    Quote Originally Posted by mark-totnes View Post
    I know this thread is old however I've just come across it with a Google search, so thought i'd post this if anybody else does...

    I believe the simplest way to disallow characters is to use the Textbox.KeyPress event.
    The below snippet handles disallowed characters AND maximum number of lines if you have a Multiline Textbox:

    Code:
        Private Sub txtAddress_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtAddress.KeyPress
            'Below Handles Disallowed Characters
            Dim DisallowedCharacters As String = "'~`{}^¨|°Ź+-[]^¨"
            If InStr(DisallowedCharacters, e.KeyChar) > 0 Then
                e.Handled = True
            End If
    
            'Below Handles max number of lines for multiline Textbox
            If txtAddress.Lines.Length >= 6 AndAlso e.KeyChar = ControlChars.Cr Then
                e.Handled = True
            End If
    
        End Sub

    Do you know how to update this to wear it uses 'IsNumeric'?

  16. #16
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,274

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

    Quote Originally Posted by intro View Post
    Do you know how to update this to wear it uses 'IsNumeric'?
    Why would you want to update this to use an old and outdated function?
    For checking if something is numeric or not you would want to use Long.TryParse(), Decimal.TryParse(), or Double.TryParse() depending on how large of a number you're looking to check. If it's simply 1 digit then Short.TryParse() would be sufficient.

    I also see in this code example it uses InStr() for checking if the incoming character is in the dis-allowed list, the code could be modified to use DisallowedCharaters.Contains(e.KeyChar) instead.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All Threads • Colors ComboBox • Fading & Gradient Form • MoveItemListBox/MoveItemListView • MultilineListBox • MenuButton • ToolStripCheckBox • Start with Windows

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width