Results 1 to 25 of 25

Thread: [RESOLVED] key down e.suppress key

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2012
    Posts
    38

    Resolved [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

  2. #2
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: key down e.suppress key

    vb Code:
    1. Private Sub txtTelephone_Change()
    2. If Not IsNumeric(txtTelephone.Text) Then lbltelephone.Caption = "Only numbers allowed!"
    3. 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

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    29

    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

  4. #4

    Thread Starter
    Member
    Join Date
    Apr 2012
    Posts
    38

    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

  5. #5
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: key down e.suppress key

    Quote Originally Posted by chrishoney View Post
    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

  6. #6

    Thread Starter
    Member
    Join Date
    Apr 2012
    Posts
    38

    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

  7. #7
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    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

  8. #8

    Thread Starter
    Member
    Join Date
    Apr 2012
    Posts
    38

    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

  9. #9

    Thread Starter
    Member
    Join Date
    Apr 2012
    Posts
    38

    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

  10. #10
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    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:
    1. Private Sub txtTelephone_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtTelephone.TextChanged
    2.         If Not IsNumeric(txtTelephone.Text) Then lbltelephone.Text = "Only numbers allowed!"
    3.     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

  11. #11

    Thread Starter
    Member
    Join Date
    Apr 2012
    Posts
    38

    Re: key down e.suppress key

    oh i c, thanks for the efforts tho, will post to appropriate forum

  12. #12
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: key down e.suppress key

    Quote Originally Posted by chrishoney View Post
    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

  13. #13

    Thread Starter
    Member
    Join Date
    Apr 2012
    Posts
    38

    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

  14. #14
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    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

  15. #15

    Thread Starter
    Member
    Join Date
    Apr 2012
    Posts
    38

    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

  16. #16
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: key down e.suppress key

    How about this?

    vb.net Code:
    1. Private Sub txtTelephone_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtTelephone.TextChanged
    2.         If Not IsNumeric(txtTelephone.Text) Then
    3.             lblTelephone.Text = "Only numbers allowed!"
    4.         Else
    5.             lblTelephone.Text = ""
    6.         End If
    7.     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

  17. #17

    Thread Starter
    Member
    Join Date
    Apr 2012
    Posts
    38

    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.

  18. #18
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    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.

  19. #19
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    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

  20. #20
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    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

  21. #21
    New Member
    Join Date
    Nov 2012
    Location
    Worcester, UK
    Posts
    9

    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

  22. #22
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: key down e.suppress key

    Quote Originally Posted by DataMiser View Post
    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

  23. #23
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: key down e.suppress key

    Quote Originally Posted by Kurfew View Post
    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?
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  24. #24

    Thread Starter
    Member
    Join Date
    Apr 2012
    Posts
    38

    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....

  25. #25
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    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
  •  



Click Here to Expand Forum to Full Width