Monday, July 15, 2013

VB6 DataGrid: Multiple Selection (Left Mouse Down + SHIFT)

Option Explicit 

Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
Private Const
VK_SHIFT = &H10
Private LastRow As Long
Private
SelectionCount As Long

'-------------------------------------------------------------------------------
'http://khoiriyyah.blogspot.com
'-------------------------------------------------------------------------------

Private Sub Form_Load()
'load database
With Adodc1
.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=biblio.mdb;Persist Security Info=False"
.RecordSource = "Select * from [titles]"
.Refresh
.Recordset.MoveFirst
End With
Set DataGrid1.DataSource = Adodc1
End Sub

Private Sub DataGrid1_SelChange(Cancel As Integer)
Call SetSelectionPlusShiftKey(DataGrid1)
End Sub

Private Sub SetSelectionPlusShiftKey(dtGrid As DataGrid)
Dim i As Integer
Dim Direction As Integer
If GetKeyState(VK_SHIFT) < 0 Then
SelectionCount = LastRow - dtGrid.Row
If SelectionCount < 0 Then
Direction = 1
Else
Direction = -1
End If
For i = 0 To SelectionCount Step -Direction
DataGrid1.SelBookmarks.Add (dtGrid.GetBookmark(i))
Next i
Else
LastRow = dtGrid.Row
End If
End Sub

Private Sub Form_Resize()
On Error Resume Next
DataGrid1.Move 0, 0, Me.ScaleWidth, Me.ScaleHeight
End Sub