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)
The above codes will also prevent copying and pasting from bringing unwanted characters.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




button on their post and leave them a comment to let them know they did a good job
Reply With Quote