Results 1 to 7 of 7

Thread: Key_Press for Listboxes

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2001
    Location
    California
    Posts
    9

    Key_Press for Listboxes

    Hello all!!!

    My listbox is linked to an SQL server DB, and the listbox is populated by a "look-up" table. So here's what I need to do:

    When the user presses a number from 1 to 50 I need the selection to jump to the listindex property that matches the number inputted.

    Listbox.style = checkbox

    For example, if the user presses 15 then the selection should jump to item that has the listindex of 15 and then check the item off.

    How would I go about this ??? Any help is very much appreciated!


    Jay
    Jay

  2. #2
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    Well this will work if the index is 99 or under. You can modify it to accept larger numbers. How it works is the user enters thier number, single digits begin with a 0, anything over 9 is entered normaly.

    For example. To enter the index of 8 you would press 0 then 8, to enter 15 you would press 1 then 5.

    VB Code:
    1. Private Sub List1_KeyPress(KeyAscii As Integer)
    2. Static Pressed As Integer, MoveToChr As Integer
    3. Pressed = Pressed + 1
    4. If Pressed = 2 Then
    5.   MoveToChr = MoveToChr & Chr(KeyAscii)
    6.    Else
    7.   MoveToChr = Chr(KeyAscii)
    8.   Exit Sub
    9. End If
    10.  Pressed = 0
    11.  List1.ListIndex = MoveToChr
    12. List1.Selected(MoveToChr) = False
    13. End Sub
    Last edited by Arc; Apr 22nd, 2002 at 07:56 PM.
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  3. #3
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    Take the code and dont even say thanks? I am saddened
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  4. #4

    Thread Starter
    New Member
    Join Date
    Oct 2001
    Location
    California
    Posts
    9
    Hi Arc,

    Thanks in advance for the code... I haven't tried it out yet because I just arrived at work. Please do not be saddened I really do appreciate your help, and all the help I get! Thanks again and I'll let you know in a few minutes how it goes!
    Jay

  5. #5
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    Ahh this a project for work? You get paid for programming with VB? Or what is your job description? If you don't mind me asking
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  6. #6

    Thread Starter
    New Member
    Join Date
    Oct 2001
    Location
    California
    Posts
    9
    Hi again Arc,

    I just finished testing out the code... it works great!!! Many thanks!
    One more thing, my listbox has a listcount of 53, and the itemdata property is zero-based. So I'm having trouble because in order to get to the first item the user must press "00" instead of "01". Also I'm having trouble with the validation of the numbers. How would I validate the keypress event so that the user does not enter a number that is "53" (because of it being zero-based) and above???

    Thanks again for all your help!!!
    Jay

  7. #7
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    this should work. It goes from 01 to 53 and displays a messagebox if the user goes over 53.

    VB Code:
    1. Private Sub List1_KeyPress(KeyAscii As Integer)
    2. Static Pressed As Integer, MoveToChr As Integer
    3. On Error Resume Next
    4. Pressed = Pressed + 1
    5. If Pressed = 2 Then
    6.   MoveToChr = MoveToChr & Chr(KeyAscii)
    7.    Else
    8.   MoveToChr = Chr(KeyAscii)
    9.   Exit Sub
    10. End If
    11.  Pressed = 0
    12. MoveToChr = MoveToChr - 1
    13. If MoveToChr > 52 Then
    14.   MsgBox "Index's above 53 not allowed"
    15.    Exit Sub
    16. End If
    17.  
    18.  List1.ListIndex = MoveToChr
    19. List1.Selected(MoveToChr) = False
    20. End Sub
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


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