VB.Net程序设计:从ListBox中拖放数据到ListBox 中。(VS2005)
运行界面图:
代码:
Public Class FrmList2List
Dim i As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Close()
End Sub
Private Sub Form3_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
For i = 1 To 5
Me.ListBox1.Items.Add(i)
Next
For i = 6 To 9
Me.ListBox2.Items.Add(i)
Next
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
If e.Button = Windows.Forms.MouseButtons.Left Then
If ListBox1.SelectedItem IsNot Nothing Then
Me.ListBox1.DoDragDrop(Me.ListBox1.SelectedItem.ToString, DragDropEffects.Copy)
Me.Label1.Text = "启动拖放数据..." & Me.ListBox1.SelectedItem
Else
Me.Label1.Text = ""
End If
End If
End Sub
Private Sub ListBox2_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox2.DragEnter
If (e.Data.GetDataPresent(DataFormats.Text)) Then
Me.Label1.Text = "有数据拖进来...托..."
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
Me.Label1.Text = ""
End If
End Sub
Private Sub ListBox2_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox2.DragDrop
If e.Data.GetDataPresent(DataFormats.Text) Then
Me.ListBox2.Items.Add(e.Data.GetData(DataFormats.Text))
Me.Label1.Text = "有数据放进来...放..."
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.Label1.Text = ""
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Me.ListBox1.ClearSelected()
End Sub
End Class