Mar 18, 2011
What I have is on my form there are several text boxes. These text boxes are for user input and attached to each text box is a Private Sub txtBox1_KeyPress function. In that function I have some validation code to check that the user is inputing only what I want them to. And if it doesnt it will pop up a message box telling them so, then it is supposed to clear the text box so the user can try again. But no matter what I try, the text box does not clear it still keeps the last character typed (the invalid one).
View 7 Replies
Here is the code for one of the text boxes.
Private Sub txtLanIp_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtLanIp.KeyPress
Dim strEmpty As String = ""
If Char.IsDigit(e.KeyChar) = True Or Char.IsPunctuation(e.KeyChar) = True Or Char.IsControl(e.KeyChar) = True Then
The commented methods at the bottom are some of the other things ive tried to make this work. The txtLanIp.text.clear() is what ive tried from the beginning. I have also tried setting the text to "", I even created an empty string and set the .Text value to it but no matter what it wont remove that character. I have also tried various trim(), len(), Select() functions, most either give me an 'index out of range error', a 'length cannot be less than zero' error or a general ExceptionOutOfBounds error. I am just tearing my hair out as to why the simple way doesnt work, there is no logical reason why and it gives no error it just doesn't clear the box.
I have also tried moving the validation to a _LostFocus but e.KeyChar isnt a valid method or property for that declaration, which doesn't surprise me but I wanted to try everything.I dont see how it would matter in this case but I am running Windows XP Pro SP3, though I also see the same behavior in Windows 7 64 bit ultimate sp1 and windows 7 64 bit pro.