Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Shift Lock

Posted on 1998-12-09
4
285 Views
Last Modified: 2010-05-03
Is there a way to simulate a Shift Lock key in VB5 or VB6? When the up or down arrow keys are pressed for my listbox control, I want the previously selected lines to remain selected - just as if I were holding down the shift key.
0
Comment
Question by:MattTriv
  • 2
  • 2
4 Comments
 
LVL 3

Expert Comment

by:jjbyers
ID: 1449071
You always want to multi-select?
0
 

Author Comment

by:MattTriv
ID: 1449072
No. Only when I am simulating the Shift Lock. The list's multiselect property is set to 'extended' and that's fine. Here's what I want to accomplish: Often, there is occasion to select many items in the list one by one. Now, the way that is done is by holding down the shift key and pressing the down arrow key over and over. I want the convenience of not having to hold the shift key down.
0
 
LVL 3

Accepted Solution

by:
jjbyers earned 410 total points
ID: 1449073
Put a checkbox and a list box on a form

paste the following code

Option Explicit

Private Sub Form_Load()
Dim i As Integer
  For i = 1 To 100
    List1.AddItem i
  Next i
End Sub

Private Sub List1_KeyDown(KeyCode As Integer, Shift As Integer)
   If Check1 Then
    If KeyCode = 40 Then
      List1.Selected(List1.ListIndex) = True
      List1.Selected(List1.ListIndex + 1) = True
      KeyCode = 0
    ElseIf KeyCode = 38 Then
      List1.Selected(List1.ListIndex) = True
      List1.Selected(List1.ListIndex - 1) = True
      KeyCode = 0
    End If
  End If
End Sub

when the checkbox is selected the up and down arrow keys will act as if shifted.

hope this helps.
0
 

Author Comment

by:MattTriv
ID: 1449074
I had to make some additions to your code, but setting KeyCode to zero is what I needed to learn, so I've got it working the way I want. Thanks for your help. Here is the modified code:

Option Explicit

Private Sub Form_Load()
Dim i As Integer
  For i = 0 To 10
    List1.AddItem i
  Next i
End Sub

Private Sub List1_KeyDown(KeyCode As Integer, Shift As Integer)
    If Check1 Then
        If KeyCode = 40 Then
            KeyCode = 0
            If List1.ListIndex = List1.ListCount - 1 Then Exit Sub
            If List1.Selected(List1.ListIndex + 1) Then
                List1.Selected(List1.ListIndex) = False
                List1.ListIndex = List1.ListIndex + 1
            Else
                List1.Selected(List1.ListIndex) = True
                List1.Selected(List1.ListIndex + 1) = True
            End If
        ElseIf KeyCode = 38 Then
            KeyCode = 0
            If List1.ListIndex = 0 Then Exit Sub
            If List1.Selected(List1.ListIndex - 1) Then
                List1.Selected(List1.ListIndex) = False
                List1.ListIndex = List1.ListIndex - 1
            Else
                List1.Selected(List1.ListIndex) = True
                List1.Selected(List1.ListIndex - 1) = True
            End If
        End If
    End If
End Sub
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question