Simulate Pressed Control Key for Multiple Selections in List Box

When selecting items in a normal list box with the MultiSelect property set to 1 – Simple or 2 – Extended,the user needs to press the control key while clicking on the items in order to continuously select multipleitems without also deselecting the items currently selected. This method lets the user select multiple itemscontinuously without pressing the control key. Place this code in a module:

 Declare Function GetKeyboardState Lib _        "user32" (pbKeyState As Byte) _        As LongDeclare Function SetKeyboardState Lib _        "user32" (lppbKeyState As Byte) _        As LongPublic Const VK_CONTROL = &H11Public KeyState(256) As Byte

Place this code in the MouseDown event procedure in a list box (List1) with MultiSelect property set aseither Simple or Extended:

 ' Sets the control key state to' "pressed"GetKeyboardState KeyState(0)KeyState(VK_CONTROL) = _        KeyState(VK_CONTROL) Or &H80SetKeyboardState KeyState(0) 

Place this code in any procedure where the pressed control key is to be released, such as theList1_LostFocus event procedure:

 ' release the control key state from ' "pressed"GetKeyboardState KeyState(0)KeyState(VK_CONTROL) = _        KeyState(VK_CONTROL) And &H7FSetKeyboardState KeyState(0)
Share the Post:
Share on facebook
Share on twitter
Share on linkedin


Recent Articles: