Solved

Shift Lock

Posted on 1998-12-09
4
287 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
[X]
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
  • 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Suggested Solutions

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…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…

734 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