|
-
Nov 21st, 2012, 03:32 AM
#1
Thread Starter
Member
[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
-
Nov 21st, 2012, 03:54 AM
#2
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
Last edited by Nightwalker83; Nov 21st, 2012 at 04:50 AM.
Reason: Fixed spelling!
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
-
Nov 21st, 2012, 04:09 AM
#3
Junior Member
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
-
Nov 21st, 2012, 04:24 AM
#4
Thread Starter
Member
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
-
Nov 21st, 2012, 04:32 AM
#5
Re: key down e.suppress key
 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.
Last edited by Nightwalker83; Nov 21st, 2012 at 04:35 AM.
Reason: Adding more!
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
-
Nov 21st, 2012, 04:46 AM
#6
Thread Starter
Member
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
-
Nov 21st, 2012, 04:50 AM
#7
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.
Last edited by Nightwalker83; Nov 21st, 2012 at 04:53 AM.
Reason: Adding more!
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
-
Nov 21st, 2012, 04:58 AM
#8
Thread Starter
Member
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
-
Nov 21st, 2012, 05:02 AM
#9
Thread Starter
Member
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
-
Nov 21st, 2012, 05:07 AM
#10
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
Last edited by Nightwalker83; Nov 21st, 2012 at 05:20 AM.
Reason: Adding more!
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
-
Nov 21st, 2012, 05:19 AM
#11
Thread Starter
Member
Re: key down e.suppress key
oh i c, thanks for the efforts tho, will post to appropriate forum
-
Nov 21st, 2012, 05:20 AM
#12
Re: key down e.suppress key
 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.
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
-
Nov 21st, 2012, 05:29 AM
#13
Thread Starter
Member
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
-
Nov 21st, 2012, 05:34 AM
#14
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?
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
-
Nov 21st, 2012, 05:43 AM
#15
Thread Starter
Member
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
-
Nov 21st, 2012, 05:50 AM
#16
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.
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
-
Nov 21st, 2012, 06:09 AM
#17
Thread Starter
Member
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
Last edited by chrishoney; Nov 21st, 2012 at 06:21 AM.
-
Nov 21st, 2012, 08:22 AM
#18
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.
-
Nov 21st, 2012, 08:33 AM
#19
Re: key down e.suppress key
Thread moved from Visual Basic 6 and Earlier
Thanks NW for the report.
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Nov 21st, 2012, 01:50 PM
#20
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
-
Nov 21st, 2012, 05:54 PM
#21
New Member
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
-
Nov 21st, 2012, 05:57 PM
#22
Re: key down e.suppress key
 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.
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
-
Nov 21st, 2012, 06:00 PM
#23
Re: key down e.suppress key
 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?
-
Nov 22nd, 2012, 02:06 AM
#24
Thread Starter
Member
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....
-
Nov 22nd, 2012, 02:56 AM
#25
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.
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
Tags for this Thread
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
|