Results 1 to 10 of 10

Thread: [2008] keychar

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2009
    Posts
    448

    [2008] keychar

    I am trying to do an keypress event that only allows numbers, backspace, and enter. I have the part that only allows numbers

    Code:
    If Not (IsNumeric(e.KeyChar)) Then
                e.Handled = True
            End If
    I need to know how to know what keychar the backspace and enter are. Can someone give me a reference?

  2. #2
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: [2008] keychar

    How about pressing them and trapping the value of e.KeyChar?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  3. #3
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: [2008] keychar

    Use the KeyDown event. That would give you more flexibility.
    vb.net Code:
    1. Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    2.     Select Case e.KeyCode
    3.         Case Keys.D0 To Keys.D9, Keys.Enter, Keys.Back
    4.             e.Handled = True
    5.     End Select
    6. End Sub
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  4. #4
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2008] keychar

    Or simply look them up in ASCII table (widely available on the Internet)
    Anyway, the ascii value for BS = 8, and enter = 13. So you need to "And" your conditions, something like this
    Code:
            Dim ch As Char = e.KeyChar
            If Not IsNumeric(ch) AndAlso ch <> ChrW(8) AndAlso ch <> ChrW(13) Then
                e.Handled = True
            End If
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  5. #5
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Re: [2008] keychar

    e.KeyChar will return ASCII value of that key so you can implement this for numeric value.

  6. #6
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: [2008] keychar

    Hi,

    You can try this foe the enter Key:

    vb Code:
    1. ' The keypressed method uses the KeyChar property to check
    2.         ' whether the ENTER key is pressed.
    3.  
    4.         ' If the ENTER key is pressed, the Handled property is set to true,
    5.         ' to indicate the event is handled.
    6.  
    7.         If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
    8.             e.Handled = True
    9.         End If

    For the Backspace use:

    vb Code:
    1. If e.KeyChar = Microsoft.VisualBasic.Chr(Keys.Back) Then
    2.             MsgBox("You pressed the Backkey")
    3.         End If

    Wkr,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2009
    Posts
    448

    Re: [2008] keychar

    Anothe quick questions what does this "<>" do I have seen it a few times but dont get it

    edit: anothe question is how do I make it do an event when a key is pressed like this
    Code:
     If e.KeyChar <> ChrW(13) Then
                btnEquals_Click()
            End If

  8. #8
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Re: [2008] keychar

    This is new question create new post for it

  9. #9
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: [2008] keychar

    Quote Originally Posted by ngreenwood6
    Anothe quick questions what does this "<>" do I have seen it a few times but dont get it

    edit: anothe question is how do I make it do an event when a key is pressed like this
    Code:
     If e.KeyChar <> ChrW(13) Then
                btnEquals_Click()
            End If
    '<>' is the equal not (the != in C#)

    btnEquals.PerformClick() is the method you want.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2009
    Posts
    448

    Re: [2008] keychar

    Thanks that worked great.

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