|
-
Nov 23rd, 2003, 06:09 PM
#1
Thread Starter
Hyperactive Member
ignore an invalid character in a text box [RESOLVED]
I am trying to prevent users from entering a character into a text box. I remember doing something like this in a beginning VB .net class a while ago and it was only one line of code in the textchanged (?) event of the textbox. From what I remember, it was a simple command to ignore the key that was pressed if it did not fit a certain criteria. Does anyone know a simple way to do this.
Last edited by JMM427; Nov 24th, 2003 at 10:53 AM.
-
Nov 23rd, 2003, 10:57 PM
#2
Frenzied Member
It is in KeyPress event, and is e.Handled=True
'Heading for the automatic overload'
Marillion, Brave, The Great Escape, 1994
'How will WE stand the FIRE TOMORROW?'
Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979
-
Nov 24th, 2003, 10:53 AM
#3
Thread Starter
Hyperactive Member
ignore character being entered [RESOLVED]
-
Nov 24th, 2003, 01:17 PM
#4
Thread Starter
Hyperactive Member
I tried the following code and it's not working. Am I missing something, or misunderstanding what the handled event does?
VB Code:
Private Sub txtMileage_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtMileage.KeyPress
If e.KeyChar.IsDigit("[0-9]") Then
e.Handled = True
Else
e.Handled = False
End If
End Sub
What I'm trying to do is ignore the key being pressed if it is not a digit?
-
Nov 24th, 2003, 01:21 PM
#5
Thread Starter
Hyperactive Member
Sorry, I tried this also:
VB Code:
Private Sub txtMileage_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtMileage.KeyPress
If e.KeyChar.IsDigit("[0-9]") = True Then
e.Handled = True
Else
e.Handled = False
End If
End Sub
Still does the same thing.
-
Nov 24th, 2003, 01:23 PM
#6
Frenzied Member
VB Code:
If Not (Char.IsDigit(e.KeyChar) Or Char.IsControl(e.KeyChar)) Then
e.Handled = True
End If
'Heading for the automatic overload'
Marillion, Brave, The Great Escape, 1994
'How will WE stand the FIRE TOMORROW?'
Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979
-
Nov 24th, 2003, 02:05 PM
#7
Thread Starter
Hyperactive Member
Finally! Thank you.
Now what if I also want it to accept a decimal separator "."?
-
Nov 24th, 2003, 03:15 PM
#8
Frenzied Member
Because the decimal point will happen only once then you have to do this:
VB Code:
If Not (Char.IsDigit(e.KeyChar) Or Char.IsControl(e.KeyChar)) Then
If Not (e.KeyChar = ".") Or (InStr(DirectCast(sender, TextBox).Text, ".", CompareMethod.Text)) Then
e.Handled = True
End If
End If
'Heading for the automatic overload'
Marillion, Brave, The Great Escape, 1994
'How will WE stand the FIRE TOMORROW?'
Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979
-
Nov 24th, 2003, 04:31 PM
#9
Thread Starter
Hyperactive Member
Thanks, that makes sense. I appreciate all your help.
-
Nov 26th, 2003, 11:34 AM
#10
Hyperactive Member
why donnt u use the method IsDigit
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
|