Support Forums
[TuToRiAl] Restricting TextBox Input (Numeric Only TextBox, etc...) - Printable Version

+- Support Forums (https://www.supportforums.net)
+-- Forum: Categories (https://www.supportforums.net/forumdisplay.php?fid=87)
+--- Forum: Coding Support Forums (https://www.supportforums.net/forumdisplay.php?fid=18)
+---- Forum: Visual Basic and the .NET Framework (https://www.supportforums.net/forumdisplay.php?fid=19)
+---- Thread: [TuToRiAl] Restricting TextBox Input (Numeric Only TextBox, etc...) (/showthread.php?tid=12438)



[TuToRiAl] Restricting TextBox Input (Numeric Only TextBox, etc...) - Resistance - 10-03-2010

Hello guys, today I will be showing you a tutorial on how to restrict textbox input, which is very useful for many things such as numeric only textboxes, textboxes for your email with the @ sign, much much more. You may not know now, but when you need it for future reference your going to be saying "Thank L3g1tWa5te!" in your head... LOL... Now for the code and procedure.

Steps:

1) Enter the following code on the top of the form.

Code:
Public Class Form1
    Dim charactersDisallowed As String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ,<>./?; :'{}[]=+-_)(*&^%$#@!~`|\uç▲ìα▲╜KÄ╤ZlÆn╘"

2) Now for TextBox1_TextChanged event code:

Code:
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

Your done. Now your textbox allows every kind of character thats not in the string "charactersDisallowed", as you can tell numbers are the only type of input allowed.


Now to make it so the textbox can only have certain characters, so the only characters that are allowed are in the string.

1) Now the same thing as before, enter the following code on the top/beginning of the form.

Code:
Public Class Form1

    Dim charactersAllowed As String = " abcdefghijklmnopqrstuvwxyz1234567890"

2) Now for TextBox2_TextChanged event code:

Code:
Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
        Dim CurrentText As String = TextBox2.Text
        Dim Input As String
        Dim SelectionIndex As Integer = TextBox2.SelectionStart
        Dim Adjust As Integer

        For x As Integer = 0 To TextBox2.Text.Length - 1
            Input = TextBox2.Text.Substring(x, 1)
            If charactersAllowed.Contains(Input) = False Then
                CurrentText = CurrentText.Replace(Input, String.Empty)
                Adjust = 1
            End If
        Next
        TextBox2.Text = CurrentText
        TextBox2.Select(SelectionIndex - Adjust, 0)
    End Sub

Thats it. Now the TextBox only allows the characters in the string, everything else is blocked for the text boxes' input.


RE: [TuToRiAl] Restricting TextBox Input (Numeric Only TextBox, etc...) - AceInfinity - 10-03-2010

From a quick glance this tutorial looks great. Good quality share Smile

Another thing to note which is to do with textbox characters is that you can set a text box to an option of hiding the characters if you want a login page or just something where you want to hide the input as you type it in. The option can be found in the property list for the textbox itself.


RE: [TuToRiAl] Restricting TextBox Input (Numeric Only TextBox, etc...) - Resistance - 10-03-2010

(10-03-2010, 09:59 PM)Infinity Wrote: From a quick glance this tutorial looks great. Good quality share Smile

Another thing to note which is to do with textbox characters is that you can set a text box to an option of hiding the characters if you want a login page or just something where you want to hide the input as you type it in. The option can be found in the property list for the textbox itself.

Ahhh yes! That would be set into the PasswordChar = "*" or that bullet point thing character.


RE: [TuToRiAl] Restricting TextBox Input (Numeric Only TextBox, etc...) - Fragma - 10-05-2010

Good tutorial. Thanks very much for sharing.


RE: [TuToRiAl] Restricting TextBox Input (Numeric Only TextBox, etc...) - Sιℓνєя - 10-05-2010

Thanks a lot what a awesome tut :O, keep up good work man.


RE: [TuToRiAl] Restricting TextBox Input (Numeric Only TextBox, etc...) - Kink - 10-16-2010

Nice tutorial, thanks for sharing.


RE: [TuToRiAl] Restricting TextBox Input (Numeric Only TextBox, etc...) - KoBE - 11-10-2010

Not trying to discredit you, Im just sharing some knowledge. A better way to do something like this (instead of looping through the text each time) would be this.

not allowed
Code:
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        Dim NotAllowed As String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ,<>./?; :'{}[]=+-_)(*&^%$#@!~`|\"
        If NotAllowed.IndexOf(e.KeyChar) <> -1 Then
            e.KeyChar = Nothing
        End If
End Sub

allowed
Code:
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        Dim Allowed As String = "abcdefghijklmnopqrstuvwxyz@"
        If Allowed.IndexOf(e.KeyChar) = -1 Then
            e.KeyChar = Nothing
        End If
End Sub