Skip to content

Instantly share code, notes, and snippets.

@UVLabs
Created July 28, 2015 17:16
Show Gist options
  • Save UVLabs/fb4c12e5d01353bd3e8b to your computer and use it in GitHub Desktop.
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.
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