Code is VB.Net, Trying to restrict users from entering anything accept numbers i
ID: 3752622 • Letter: C
Question
Code is VB.Net, Trying to restrict users from entering anything accept numbers into the text boxes using a CASE statement, but I keep getting System.EventArgs errors.
My Code:
Protected Sub ValueBox1_TextChanged(sender As Object, e As EventArgs) Handles ValueBox1.TextChanged
Select Case e.KeyChar
Case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", vbBack
e.Handled = False
Case Else
e.Handled = True
End Select
End Sub
Errors = 'KeyChar' and 'Handled' are not members of 'System.EventArgs'
I have tried changing it to a KeyPress event
but then the message changes to "KeyPress" cannot be found.
How can I restrict users from entering anything accet numbers??
Explanation / Answer
Following is the code from which user can be restricted from anything accept number:
Public Class MainForm
Dim characterDsiallowed 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
For x As Integer = 0 To TextBox1.Text.Substring-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
End Class