|
-
Feb 9th, 2013, 05:31 PM
#12
New Member
Re: Restrict TextBox to only certain characters, numeric or symbolic
 Originally Posted by Vectris
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|