DragDrop definitely becoming a DRAGdrop

Hi,

OK, I've got a Sheridan Data Grid control and a normal VB ListBox control.

What I'd like to do is drag from the listbox into the grid and place the contents of the listbox into the CELL the mouse is over when the drag drop occurs.

Any ideas how to do this with the X,Y screen positions or any other way?
Dan1712Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jelliott720Commented:
' Sheridan Grid Drag & Drop Example
'
' Developed with VB 6 (SP3) & Sheridan Data Widgets v3.11 build 005
'
' The key is to determine where (row and column) in the grid
' the user wants to drop the source data from the listbox
' this is accomplished in the grdEmployee_DragOver procedure
' using the grids' RowContaining & ColContaining methods
' e.g.
' lngGridRow = grdEmployee.RowContaining(Y)
' lngGridCol = grdEmployee.ColContaining(X)

' Begin by creating a standard VB form then add a grid to the
' form and give it the name grdEmployee then
' add three labels to the form naming them lblcurRow, lblcurCol
' and lblDragBox.
'
' Now add two listboxes to the form naming them lstDeptName and lstRate

' Add the following variable in the general declarations
'
 Dim mintCnt As Integer
 Dim lngGridRow As Long
 Dim lngGridCol As Long

'
'  Add the following procedure in the general declarations
'
Sub DrawBox(ctl As Control, YCoord As Single)
    Dim sngCharHeight As Single
    '
    ' Use the Label lblDragBox to simulate a Drag Icon
    '
    sngCharHeight = Me.TextHeight("A") ' Calc std Text height
    With lblDragBox
         .Move ctl.Left, _
               ctl.Top + YCoord - sngCharHeight \ 2, _
               ctl.Height, _
               sngCharHeight
         .Drag
         .Tag = ctl.Name             ' Save the listbox name
    End With
End Sub

'
' In the Form_DragOver procedure add the following code
'
Private Sub Form_DragOver(Source As Control, X As Single, Y As Single, State As Integer)
' Alter the cusors appearance over the form
If State = 0 Then Source.MousePointer = vbNoDrop
If State = 1 Then Source.MousePointer = vbDefault
End Sub

'
'
' In the Form_Load procedure add the following code
'
Private Sub Form_Load()
     '
     ' Set the display properties for key controls
     '
     With Me
        .Caption = "Sheridan Grid Drag & Drop Example"
        .Width = 8190: .Height = 4770
        .Move (Screen.Width - .Width) / 2, (Screen.Height - .Height) / 2
     End With

     With grdEmployee
         .Width = 5790: .Height = 2655
         .Move 255, 420
         .DataMode = ssDataModeAddItem
     End With

     With lstDeptName
          .Width = 1690: .Height = 1230
          .Left = (Width - .Width) - 400
          .Top = grdEmployee.Top
     End With

     With lstRate
          .Width = 1690: .Height = 1230
          .Left = (Width - .Width) - 400
          .Top = lstDeptName.Top + lstDeptName.Height + 100
     End With
    '
    ' Populate the two listboxes with data
    For mintCnt = 1 To 10
        lstDeptName.AddItem "Dept" & _
        Format$(mintCnt, "00") ' Create a Dept Name

        lstRate.AddItem Format$(mintCnt * 1.5, "currency")  ' Create a Rate Amt
    Next

End Sub


Private Sub cmdExit_Click()

    Unload Me
    End
End Sub
'
' In the grdEmployee_DragDrop procedure add the following code
'
Private Sub grdEmployee_DragDrop(Source As Control, X As Single, Y As Single)
 With grdEmployee
    '
    ' For this example make sure we stay within the Grid...
    ' however, it would be a simple matter to add a new row
    ' at this point...
    If lngGridRow < 0 Or lngGridRow > .Rows Then
       Beep
       MsgBox "Invalid Destination Row selected..", vbCritical, "Selection Error"
       Exit Sub
    End If
    If lngGridCol < 1 Or lngGridCol > .Cols Then
       Beep
       MsgBox "Invalid Destination Col selected..", vbCritical, "Selection Error"
       Exit Sub
    End If

    .SelBookmarks.Add .RowBookmark(lngGridRow) ' Hilight the row
    .Bookmark = .RowBookmark(lngGridRow)       ' Select the row

    '
    ' Place the Listbox data in the
    ' appropriate Grid Column
    If Source.Tag = "lstDeptName" Then
       .Columns(1).Text = lstDeptName
    ElseIf Source.Tag = "lstRate" Then
       .Columns(2).Text = lstRate
    End If

End With

End Sub
'
' In the grdEmployee_DragOver procedure add the following code
'
Private Sub grdEmployee_DragOver(Source As Control, X As Single, Y As Single, State As Integer)
'
' Save the current row & column locations
' displaying the locations as the cursor moves
' over the grid
lngGridRow = grdEmployee.RowContaining(Y)
lblCurRow.Caption = "Current Row " & lngGridRow
'
lngGridCol = grdEmployee.ColContaining(X)
lblCurCol.Caption = "Current Col " & lngGridCol
DoEvents

End Sub

'
' Change a few key properties for the Grid in the
' grdEmployee_InitColumnProps procedure
'
Private Sub grdEmployee_InitColumnProps()
With grdEmployee

    .Caption = vbNullString

    .Columns.Add 0        ' Add two columns to the Grid
    .Columns.Add 1

    .Columns(0).Caption = "Employee"
    .Columns(1).Caption = "Dept"
    .Columns(2).Caption = "Rate"

    .MaxSelectedRows = 1

    .SelectByCell = True
    .SelectTypeCol = ssSelectionTypeNone
    .SelectTypeRow = ssSelectionTypeSingleSelect

    .AllowUpdate = False

    .ScrollBars = ssScrollBarsVertical

' Populate the grdEmployee Sheridan Grid with Names

     .AddItem ("Joe Smith")
     .AddItem ("John Adams")
     .AddItem ("Judy Jackson")
     .AddItem ("Larry Johnson")
     .AddItem ("Mike Hoehne")
     .AddItem ("Jesse Elliott")
     .AddItem ("Pam Divario")
     .AddItem ("Joyce Pettybone")
     .AddItem ("Pam Divario")

End With


End Sub
'
' In the lstDeptName_MouseDown procedure add the following code
'
Private Sub lstDeptName_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Call DrawBox(Me.ActiveControl, Y)
End Sub
'
' In the lstRate_MouseDown procedure add the following code
'
Private Sub lstRate_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Call DrawBox(Me.ActiveControl, Y)
End Sub
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.