Solved

Shift Lock

Posted on 1998-12-09
4
281 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now