Results 1 to 3 of 3

Thread: [RESOLVED] Format a char from e.KeyCode etc.

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2004
    Posts
    99

    [RESOLVED] Format a char from e.KeyCode etc.

    I have a keydown event that looks like this

    VB Code:
    1. If e.KeyCode <> Windows.Forms.Keys.Up And e.KeyCode <> Windows.Forms.Keys.Down And e.KeyCode <> Windows.Forms.Keys.Enter Then
    2.             Me.txtSearchBox.Focus()
    3.             Me.txtSearchBox.Text = e.KeyCode.ToString
    4.             Me.txtSearchBox.SelectionStart = 1
    5.         End If

    This works fine, except for when you use the number pad. For instance, if you hit '2' on the number pad, the textbox would show 'NumPad2'. I want it to just display the '2'. I have tried e.KeyCode, e.KeyData, e.KeyValue, and all of their ToString() functions, but none of them return what I want. Does anyone know how I can fix this?

    Thanks for any help!
    Last edited by thecow95; Jul 26th, 2005 at 12:41 PM.

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Format a char from e.KeyCode etc.

    there may be a simpler solution, but you can simply extract the number from the keyname
    VB Code:
    1. Public Function GetNumbersInString(ByVal text As String) As String
    2.         Dim C() As Char = text.ToCharArray
    3.         Dim temp As String = String.Empty
    4.         For i As Integer = 0 To C.Length - 1
    5.             If Char.IsDigit(C(i)) Then
    6.                 temp &= C(i)
    7.             End If
    8.         Next
    9.         Return temp
    10.     End Function
    i actually use this function for one reason or another in just about all my apps

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2004
    Posts
    99

    Re: Format a char from e.KeyCode etc.

    I was trying to find an easier way, but had no luck.

    This function gets me there though, I appreciate it!

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