February 17th, 2000, 09:23 AM
Drag and Drop in ListView
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
February 18th, 2000, 01:33 AM
Re: Drag and Drop in ListView
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)
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..
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)
for i = 0 to 10
set litem = me.ListView1.ListItems.Add
.Text = Str(i)
.SubItems(1) = "( 1, " & Str(i) & ")"
.SubItems(2) = "( 2, " & Str(i) & ")"
.SubItems(3) = "( 3, " & Str(i) & ")"
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 "Calling get data in oledrag-drop in form:"
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)
private Sub ListView1_OLECompleteDrag(Effect as Long)
Debug.print "OLECompleteDrag in LV"
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"
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"
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
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
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)
Debug.print "OleSetData fn added items:="; Data.Files.Count
private Sub ListView1_OLEStartDrag(Data as ComctlLib.DataObject, AllowedEffects as Long)
Debug.print "OleStartDrag in LV: Now format=Text? "
'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
Click Here to Expand Forum to Full Width
This is a Codeguru.com survey!