hhe
Kode program di bawah ini adalah untuk mengubah urutan item pada listbox dengan cara drag n drop .
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ListBox1.AllowDrop = True
ListBox2.AllowDrop = True
End Sub
ListBox1.AllowDrop = True
ListBox2.AllowDrop = True
End Sub
Private Sub listbox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDown, ListBox2.MouseDown
Dim lst As ListBox = DirectCast(sender, ListBox)
If e.Button = Windows.Forms.MouseButtons.Left Then
Dim index As Integer = lst.IndexFromPoint(e.X, e.Y)
If index <> ListBox.NoMatches Then
Dim item As String = lst.Items(index)
Dim drop_effect As DragDropEffects = lst.DoDragDrop(lst.Items(index), DragDropEffects.Move Or DragDropEffects.Copy)
If drop_effect = DragDropEffects.Move Then
If lst.Items(index) = item Then
lst.Items.RemoveAt(index)
Else
lst.Items.RemoveAt(index + 1)
End If
End If
End If
End If
End Sub
Dim lst As ListBox = DirectCast(sender, ListBox)
If e.Button = Windows.Forms.MouseButtons.Left Then
Dim index As Integer = lst.IndexFromPoint(e.X, e.Y)
If index <> ListBox.NoMatches Then
Dim item As String = lst.Items(index)
Dim drop_effect As DragDropEffects = lst.DoDragDrop(lst.Items(index), DragDropEffects.Move Or DragDropEffects.Copy)
If drop_effect = DragDropEffects.Move Then
If lst.Items(index) = item Then
lst.Items.RemoveAt(index)
Else
lst.Items.RemoveAt(index + 1)
End If
End If
End If
End If
End Sub
Private Sub listbox1_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox1.DragOver, ListBox2.DragOver
If Not (e.Data.GetDataPresent(GetType(System.String))) Then
e.Effect = DragDropEffects.None
ElseIf (e.AllowedEffect And DragDropEffects.Move) = DragDropEffects.Move Then
e.Effect = DragDropEffects.Move
End If
End Sub
If Not (e.Data.GetDataPresent(GetType(System.String))) Then
e.Effect = DragDropEffects.None
ElseIf (e.AllowedEffect And DragDropEffects.Move) = DragDropEffects.Move Then
e.Effect = DragDropEffects.Move
End If
End Sub
Private Sub listbox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox1.DragDrop, ListBox2.DragDrop
If e.Data.GetDataPresent(GetType(System.String)) Then
If (e.Effect = DragDropEffects.Copy) Or (e.Effect = DragDropEffects.Move) Then
Dim lst As ListBox = DirectCast(sender, ListBox)
Dim item As Object = CType(e.Data.GetData(GetType(System.String)), System.Object)
Dim pt As Point = lst.PointToClient(New Point(e.X, e.Y))
Dim index As Integer = lst.IndexFromPoint(pt.X, pt.Y)
If index = ListBox.NoMatches Then
lst.Items.Add(item)
Else
lst.Items.Insert(index, item)
End If
End If
End If
End Sub
If e.Data.GetDataPresent(GetType(System.String)) Then
If (e.Effect = DragDropEffects.Copy) Or (e.Effect = DragDropEffects.Move) Then
Dim lst As ListBox = DirectCast(sender, ListBox)
Dim item As Object = CType(e.Data.GetData(GetType(System.String)), System.Object)
Dim pt As Point = lst.PointToClient(New Point(e.X, e.Y))
Dim index As Integer = lst.IndexFromPoint(pt.X, pt.Y)
If index = ListBox.NoMatches Then
lst.Items.Add(item)
Else
lst.Items.Insert(index, item)
End If
End If
End If
End Sub
Kurang lebih itulah kode nya. Selamat mencoba ...
Tidak ada komentar:
Posting Komentar