Created
July 28, 2015 17:16
-
-
Save UVLabs/fb4c12e5d01353bd3e8b to your computer and use it in GitHub Desktop.
From http://www.vbforums.com/showthread.php?570438-Restrict-TextBox-to-only-certain-characters-numeric-or-symbolic Disallow characters in textbox, could apply to other field types.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment