-
[RESOLVED] key down e.suppress key
i have a telephone number textbox that i have used the key down event to suppress any other keys besides 0-9 and the backspace key. i have a label beside it.
i want to display a message on lbltelephone when the user presses an alphabetic key that the textbox is for numbers only,and the message should disapper when the user inputs correct values...
currently i have
e.suppresskeypress=true
lbltelephone.text="no"
but the message does not disappear when user enters correct values and even if focus is lost
-
Re: key down e.suppress key
vb Code:
Private Sub txtTelephone_Change()
If Not IsNumeric(txtTelephone.Text) Then lbltelephone.Caption = "Only numbers allowed!"
End Sub
-
Re: key down e.suppress key
You dont need to display a message stating that the user has pressed anything other than a numerical digit.
Simply limit the text box to accept numbers only.
Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0
End Sub
-
Re: key down e.suppress key
nightwalker. ..i have used the text changed event and your code and its displaying the message when i type in a number like 4, my programming environment is visual studio
-
Re: key down e.suppress key
Quote:
Originally Posted by
chrishoney
nightwalker. ..i have used the text changed event and your code and its displaying the message when i type in a number like 4, my programming environment is visual studio
Are you using vb6? If so there must be an error somewhere! Make sure you include the "not" in the above code.
Edit:
@ Dr. Evil,
You should always notify the user of things such as Chris wants to do.
-
Re: key down e.suppress key
i am using visual studio, maybe thats the problem and i am not forgetting the not
Private Sub txtTel_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtTel.TextChanged
If Not IsNumeric(txtTel) Then lbltelephone.text = "only"
End Sub
-
Re: key down e.suppress key
You need the .Text on the end of "txtTel"! I have fixed my post above.
Edit:
lblTelephone should be .Caption not .Text.
-
Re: key down e.suppress key
have added the .text now no message is displayed when when i press other keys(a-z). it accepts only numbers though,strangely when i delete all the numbers thats when the message is displayed
-
Re: key down e.suppress key
i also have this keydown event for the same textbox, does it have any impact on th etext changed event we are trying to handle
Private Sub txtTel_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtTel.KeyDown
If e.KeyCode = Keys.D0 Or e.KeyCode = Keys.D1 Or e.KeyCode = Keys.D2 Or e.KeyCode = Keys.D3 Or _
e.KeyCode = Keys.D4 Or e.KeyCode = Keys.D5 Or e.KeyCode = Keys.D6 Or e.KeyCode = Keys.D7 Or _
e.KeyCode = Keys.D8 Or e.KeyCode = Keys.D9 Or e.KeyCode = Keys.Back Then
Else
e.SuppressKeyPress = True
End If
If e.KeyCode = Keys.Escape Then
txtTel.Text = ""
End If
If e.KeyData = Keys.Return Then
txtPerson.Focus()
End If
End Sub
-
Re: key down e.suppress key
That is vb.net code not vb6.
Edit:
Here is the vb.Net version of the above code.
vb Code:
Private Sub txtTelephone_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtTelephone.TextChanged
If Not IsNumeric(txtTelephone.Text) Then lbltelephone.Text = "Only numbers allowed!"
End Sub
-
Re: key down e.suppress key
oh i c, thanks for the efforts tho, will post to appropriate forum
-
Re: key down e.suppress key
Quote:
Originally Posted by
chrishoney
oh i c, thanks for the efforts tho, will post to appropriate forum
I have added to my previous post and asked a mod to move the thread.
-
Re: key down e.suppress key
thats exactly what i did when you posted the solution in vb6, and it brought up those results i posted
-
Re: key down e.suppress key
I'm not sure how the code you posted in post #9 would affect the operation of the code I posted in post #10. If you comment the code in post #9 out does the code in post #10 work?
-
Re: key down e.suppress key
if i comment out the key down event handler the textbox accepts all user input and it will display the message forever even when i type in numbers
-
Re: key down e.suppress key
How about this?
vb.net Code:
Private Sub txtTelephone_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtTelephone.TextChanged
If Not IsNumeric(txtTelephone.Text) Then
lblTelephone.Text = "Only numbers allowed!"
Else
lblTelephone.Text = ""
End If
End Sub
without using the keydown event.
-
Re: key down e.suppress key
on keydown event after e.suppresskey = true i have added if e.suppresskey=true then
lbltel.text="nums pliz" else
lbltel.text=""
end if
and with the code u supplied on text changed this is what is being fired. if ii comment out yr code this doesnt work properly
-
Re: key down e.suppress key
You should not be changing the text in the text box, As a user I would be pretty upset with a program where I had typed in the first 9 digits and then hit an invalid key by mistake only to have the program wipe out everythig I typed and replace it with a message. Definitely not a good idea.
You should also as a rule never change the text in the text changed event as this causes the event to fire again due to the change.
You should be supressing the unwanted characters in the key down event and if you really want to show a message then do so in a seperate label. I typically use red to get the users attention but more often I will just sound a ding when an invalid key is hit.
There should be no need for the code you have in the text changed event.
-
Re: key down e.suppress key
Thread moved from Visual Basic 6 and Earlier
Thanks NW for the report. :thumb:
-
Re: key down e.suppress key
It looks to me you're trying to emulate what the error provider control does, so why don't you use an error provider control? It does what you need and more all by itself, see this simple video tutorial:
VB.NET Working with an ErrorProvider
-
Re: key down e.suppress key
I've just had to do exactly the same thing in VS 2008 and it works perfectly.
Here's what i used, it won't allow them to enter anything but numbers and backspace, hope it's of some help.
Code:
Private Sub txt()_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt().KeyPress
If (Microsoft.VisualBasic.Asc(e.KeyChar) < 48) _
Or (Microsoft.VisualBasic.Asc(e.KeyChar) > 57) Then
e.Handled = True
End If
If (Microsoft.VisualBasic.Asc(e.KeyChar) = 8) Then
e.Handled = False
End If
-
Re: key down e.suppress key
Quote:
Originally Posted by
DataMiser
You should not be changing the text in the text box
It's a label that he has changing the text I think the text box is only for detecting the numbers, etc.
-
Re: key down e.suppress key
Quote:
Originally Posted by
Kurfew
I've just had to do exactly the same thing in VS 2008 and it works perfectly.
Here's what i used, it won't allow them to enter anything but numbers and backspace, hope it's of some help.
Code:
Private Sub txt()_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt().KeyPress
If (Microsoft.VisualBasic.Asc(e.KeyChar) < 48) _
Or (Microsoft.VisualBasic.Asc(e.KeyChar) > 57) Then
e.Handled = True
End If
If (Microsoft.VisualBasic.Asc(e.KeyChar) = 8) Then
e.Handled = False
End If
Try copying some text with letters and then paste it into the textbox. Does the code work?
-
Re: key down e.suppress key
yes finally its working, have used the keydown event instead and incooporated the text changed event as well as the leave event and the label displays a message when characters are pressed otherwise it takes only numbers.
many thanks guys for the guidance many more to nightwalker tho....
-
Re: key down e.suppress key
Don't forget to mark the thread "Resolved" if you have finish with it! Also, you might consider giving rep to those people who have help you.