Solved

Shift Lock

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…

932 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

14 Experts available now in Live!

Get 1:1 Help Now