Hi Thomas,
It's a real problem. I'd been trying to get this figured out for some time,
and I finally turned to a kludge: listviews in smallicon view, which is a
listbox for all intents and purposes. Then I created a control that places
2 such listviews on a form with the drag/drop capabilities built in.
The basic code for this is below, but if you like, I can send you the
solution so you can see all the code and, once compiled, you can add the
double listviews to your toolbox.
HTH,
Bernie Yaeger
Imports System.Componen tModel
Public Class listviewdragdro p
Inherits System.Windows. Forms.UserContr ol
Enum boxchoice As Integer
left
right
End Enum
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeCompo nent()
'Add any initialization after the InitializeCompo nent() call
End Sub
'UserControl1 overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Disp ose()
End If
End If
MyBase.Dispose( disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.Componen tModel.IContain er
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents ListView1 As System.Windows. Forms.ListView
Friend WithEvents ListView2 As System.Windows. Forms.ListView
<System.Diagnos tics.DebuggerSt epThrough()> Private Sub InitializeCompo nent()
Me.ListView1 = New System.Windows. Forms.ListView
Me.ListView2 = New System.Windows. Forms.ListView
Me.SuspendLayou t()
'
'ListView1
'
Me.ListView1.Al lowDrop = True
Me.ListView1.Lo cation = New System.Drawing. Point(8, 8)
Me.ListView1.Na me = "ListView1"
Me.ListView1.Si ze = New System.Drawing. Size(121, 120)
Me.ListView1.Ta bIndex = 0
Me.ListView1.Ta bStop = False
Me.ListView1.Vi ew = System.Windows. Forms.View.Smal lIcon
'
'ListView2
'
Me.ListView2.Al lowDrop = True
Me.ListView2.Lo cation = New System.Drawing. Point(144, 8)
Me.ListView2.Na me = "ListView2"
Me.ListView2.Si ze = New System.Drawing. Size(121, 120)
Me.ListView2.Ta bIndex = 1
Me.ListView2.Ta bStop = False
Me.ListView2.Vi ew = System.Windows. Forms.View.Smal lIcon
'
'listviewdragdr op
'
Me.AllowDrop = True
Me.Controls.Add (Me.ListView2)
Me.Controls.Add (Me.ListView1)
Me.Name = "listviewdragdr op"
Me.Size = New System.Drawing. Size(272, 136)
Me.ResumeLayout (False)
End Sub
#End Region
Function selecteditem(By Val x As boxchoice, ByVal i As Integer) As String
If x = boxchoice.left Then
selecteditem = ListView1.Items (i).Text
Else
selecteditem = ListView2.Items (i).Text
End If
'selecteditem = ListView2.Items (i).Text
End Function
Function countselected(B yVal x As boxchoice) As Integer
If x = boxchoice.left Then
countselected = ListView1.Items .Count
Else
countselected = ListView2.Items .Count
End If
End Function
Function addleft(ByVal t As String) As ListViewItem
addleft = ListView1.Items .Add(t)
End Function
Function addright(ByVal t As String) As ListViewItem
addright = ListView2.Items .Add(t)
End Function
Private Sub ListView_ItemDr ag(ByVal sender As System.Object, ByVal e As
System.Windows. Forms.ItemDragE ventArgs) Handles ListView1.ItemD rag,
ListView2.ItemD rag
Dim myitem As ListViewItem
Dim myitems(sender. SelectedItems.C ount - 1) As ListViewItem
Dim i As Integer = 0
For Each myitem In sender.Selected Items
myitems(i) = myitem
i = i + 1
Next
sender.allowdro p = False
sender.dodragdr op(New DataObject("Sys tem.Windows.For ms.ListViewItem ()",
myitems), DragDropEffects .Move)
End Sub
Private Sub ListView_DragEn ter(ByVal sender As System.Object, ByVal e As
System.Windows. Forms.DragEvent Args) Handles ListView1.DragE nter,
ListView2.DragE nter
If e.Data.GetDataP resent("System. Windows.Forms.L istViewItem()") Then
e.Effect = DragDropEffects .Move
Else
e.Effect = DragDropEffects .None
End If
End Sub
Private Sub ListView1_DragD rop(ByVal sender As System.Object, ByVal e As
System.Windows. Forms.DragEvent Args) Handles ListView1.DragD rop
Dim myitem As ListViewItem
Dim myitems() As ListViewItem =
e.Data.GetData( "System.Windows .Forms.ListView Item()")
Dim i As Integer = 0
For Each myitem In myitems
sender.Items.Ad d(myitems(i).Te xt)
ListView2.Items .Remove(ListVie w2.SelectedItem s.Item(0))
i = i + 1
Next
ListView1.Allow Drop = True
ListView2.Allow Drop = True
End Sub
Private Sub ListView2_DragD rop(ByVal sender As System.Object, ByVal e As
System.Windows. Forms.DragEvent Args) Handles ListView2.DragD rop
Dim myitem As ListViewItem
Dim myitems() As ListViewItem =
e.Data.GetData( "System.Windows .Forms.ListView Item()")
Dim i As Integer = 0
For Each myitem In myitems
sender.Items.Ad d(myitems(i).Te xt)
ListView1.Items .Remove(ListVie w1.SelectedItem s.Item(0))
i = i + 1
Next
ListView1.Allow Drop = True
ListView2.Allow Drop = True
End Sub
Private Sub ListView_MouseD own(ByVal sender As Object, ByVal e As
System.Windows. Forms.MouseEven tArgs) Handles ListView1.Mouse Down,
ListView2.Mouse Down
ListView1.Allow Drop = True
ListView2.Allow Drop = True
End Sub
Private Sub ListView_MouseU p(ByVal sender As Object, ByVal e As
System.Windows. Forms.MouseEven tArgs) Handles ListView1.Mouse Up,
ListView2.Mouse Up
ListView1.Allow Drop = True
ListView2.Allow Drop = True
End Sub
End Class
<th*****@msala. net> wrote in message
news:42******** **************@ news.newsdemon. com...
Has anyone got some sample code to do drag and drop from one listbox to
another listbox using VB.Net 2005. The below code works for draging and
droping one at a time, but not for multiselected items. I tried setting
up
an array to capture the selected items and then move them with the
dragndrop
code, but after selecting the items when the user clicks on the items to
drag them the selection goes back to one item. Also I have code for the
listbox doubleclick event that moves whatever item is doubleclicked in one
listbox to the other. This worked fine until I added the dragndrop code to
the mousedown event, now instead of a doubleclick event I get two
mousedown
events. I am sure someone has done this and I would appreciate seeing the
code.
Thanks
Thomas
Private Sub lstSelected_Dra gDrop(ByVal sender As Object, ByVal e As
System.Windows. Forms.DragEvent Args) Handles lstSelected.Dra gDrop
lstSelected.Ite ms.Add(e.Data.G etData(DataForm ats.Text).ToStr ing)
lstAvailable.It ems.Remove(e.Da ta.GetData(Data Formats.Text).T oString)
End Sub
Private Sub lstAvailable_Dr agDrop(ByVal sender As Object, ByVal e As
System.Windows. Forms.DragEvent Args) Handles lstAvailable.Dr agDrop
lstAvailable.It ems.Add(e.Data. GetData(DataFor mats.Text).ToSt ring)
lstSelected.Ite ms.Remove(e.Dat a.GetData(DataF ormats.Text).To String)
End Sub
Private Sub lstSelected_Dra gEnter(ByVal sender As Object, ByVal e As
System.Windows. Forms.DragEvent Args) Handles lstSelected.Dra gEnter
If (e.Data.GetData Present(DataFor mats.Text)) Then
e.Effect = DragDropEffects .Move
Else
e.Effect = DragDropEffects .None
End If
End Sub
Private Sub lstAvailable_Dr agEnter(ByVal sender As Object, ByVal e As
System.Windows. Forms.DragEvent Args) Handles lstAvailable.Dr agEnter
If (e.Data.GetData Present(DataFor mats.Text)) Then
e.Effect = DragDropEffects .Move
Else
e.Effect = DragDropEffects .None
End If
End Sub
Private Sub lstAvailable_Mo useDown(ByVal sender As Object, ByVal e As
System.Windows. Forms.MouseEven tArgs) Handles lstAvailable.Mo useDown
strDragDrop = lstAvailable.Te xt
lstAvailable.Do DragDrop(strDra gDrop, DragDropEffects .Move)
End Sub
Private Sub lstSelected_Mou seDown(ByVal sender As Object, ByVal e As
System.Windows. Forms.MouseEven tArgs) Handles lstSelected.Mou seDown
strDragDrop = lstSelected.Tex t
lstSelected.DoD ragDrop(strDrag Drop, DragDropEffects .Move)
End Sub
Private Sub lstAvailable_Mo useDoubleClick( ByVal sender As Object, ByVal e
As
System.EventArg s) Handles lstAvailable.Do ubleClick
lstSelected.Ite ms.Add(lstAvail able.SelectedIt em)
lstAvailable.It ems.Remove(lstA vailable.Select edItem)
End Sub
Private Sub lstSelected_Dou bleClick(ByVal sender As Object, ByVal e As
System.EventArg s) Handles lstSelected.Dou bleClick
lstAvailable.It ems.Add(lstSele cted.SelectedIt em)
lstSelected.Ite ms.Remove(lstSe lected.Selected Item)
End Sub
--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.c om<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access