Drag and Drop in ListView
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Drag and Drop in ListView

  1. #1
    Join Date
    Dec 1999
    Location
    USA
    Posts
    3

    Drag and Drop in ListView

    Hi,
    I am trying to drag and drop multiple items from a listview on to a treeview node which will ultimately be updated in a corresponding table in the database(just like selecting multiple files in windows explorer and copying to different nodes).My problem is I am not able to drag all the items that I select.When I drag only one Item gets selected,Could somebody help me with this problem or give me some suggestions.Any suggestions will be highly appreciated.
    Thanks in Advance
    Cheers,
    Suvipra


  2. #2
    Join Date
    May 1999
    Location
    Omika, Japan
    Posts
    729

    Re: Drag and Drop in ListView

    Hi,

    Nice question. I hadn't worked on this before, so it gave me a chance to.
    ------
    Here is a working example. It wont show all the points, but enough to start you off i guess.

    Add a form, and a listview, default name.
    Change the Form's OleDropMode=1
    and LV's OleDragMode=1 (Automatic)
    and MultiSelect=True
    Cut and paste this code and run.

    Select few elements from LV and drag and drop on to the Form.
    Check the debug window outputs, to know what is happening.
    =======
    You have to override the OleStartDrag event, to add your own data to Data object. Then process and pass on what ever data at SetData event.

    It is understood that the drop target knows what it is going to get. So we will pass the Selected item and the 3rd column's contents ( why on earth!!). I.e the format of the data passed should be of mutually understandable (from Drag source to Drop target).

    In your case of DBrecords how you will do this is ... well.. your problem! :-)

    --
    oops, i hit send, before pasting the code..lucky they have "preview post" facility..


    option Explicit

    private Sub Form_Load()
    Dim i as Integer
    Dim litem as ListItem
    ListView1.View = lvwReport
    for i = 0 to 3
    ListView1.ColumnHeaders.Add , , "Col" & Str(i)
    next i
    for i = 0 to 10
    set litem = me.ListView1.ListItems.Add
    With litem
    .Text = Str(i)
    .SubItems(1) = "( 1, " & Str(i) & ")"
    .SubItems(2) = "( 2, " & Str(i) & ")"
    .SubItems(3) = "( 3, " & Str(i) & ")"
    End With
    next i

    End Sub

    private Sub Form_OLEDragDrop(Data as DataObject, Effect as Long, Button as Integer, Shift as Integer, x as Single, y as Single)
    Debug.print "OleDragDrop in FOrm: with format=text?";
    Debug.print Data.GetFormat(vbCFText)
    Debug.print "Calling get data in oledrag-drop in form:"
    Debug.print Data.GetData(vbCFText)
    If Data.GetFormat(vbCFFiles) = true then
    ' if the format is also files, then it is assumed that the selected list is in
    '
    Dim i as Integer
    for i = 0 to Data.Files.Count - 1
    Debug.print Data.Files.Item(i + 1)
    next i
    End If
    End Sub

    private Sub ListView1_OLECompleteDrag(Effect as Long)
    Debug.print "OLECompleteDrag in LV"
    End Sub

    private Sub ListView1_OLEDragDrop(Data as ComctlLib.DataObject, Effect as Long, Button as Integer, Shift as Integer, x as Single, y as Single)
    Debug.print "OLEDragDrop in LV"
    End Sub

    private Sub ListView1_OLEDragOver(Data as ComctlLib.DataObject, Effect as Long, Button as Integer, Shift as Integer, x as Single, y as Single, State as Integer)
    'Debug.print "OleDragOver in LV"
    End Sub

    private Sub ListView1_OLEGiveFeedback(Effect as Long, DefaultCursors as Boolean)
    static times as Integer
    If times < 3 then
    Debug.print "OLEGiveFeedBack in LV"
    times = times + 1
    End If
    End Sub

    private Sub ListView1_OLESetData(Data as ComctlLib.DataObject, DataFormat as Integer)
    Debug.print "OleSetData in LV: Data format ="; DataFormat
    If DataFormat = vbCFText then
    ' we can override the default LV behaviour here: May be?
    Data.SetData "new overridden text value", DataFormat
    End If
    Dim lvitem as ListItem
    Dim i as Integer
    for Each lvitem In ListView1.ListItems
    If lvitem.Selected = true then
    Data.Files.Add lvitem.Text & " | " & lvitem.SubItems(3)
    End If
    next lvitem
    Debug.print "OleSetData fn added items:="; Data.Files.Count
    End Sub

    private Sub ListView1_OLEStartDrag(Data as ComctlLib.DataObject, AllowedEffects as Long)
    Debug.print "OleStartDrag in LV: Now format=Text? "
    Debug.print Data.GetFormat(vbCFText)
    'Data.Clear
    'Data.SetData ListView1.SelectedItem.Text, vbCFText
    Data.SetData , vbCFFiles
    AllowedEffects = vbDropEffectCopy

    '' IN testing i noticed that if you set the AllowedEffects to vbDropEffectMove
    '' LV automatically deletes the last selected/active item.
    '' so that means we have to check the OleCompleteDrag event to remove all
    '' the selected items if Effect is "Move".

    '' Adding data can be deligated to a later point: till OleSetData event:
    ' Dim lvitem as ListItem
    ' Dim i as Integer
    ' for Each lvitem In ListView1.ListItems
    ' If lvitem.Selected = true then
    ' Data.Files.Add lvitem.Text
    ' End If
    ' next lvitem
    ' Debug.print "OleSetData fn added items:="; Data.Files.Count
    End Sub




    RK

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center