不会写C#的,附vb代码如下,仅供参考:
Public Class Form2 Private Sub TreeView1_ItemDrag(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemDragEventArgs) Handles TreeView1.ItemDrag
TreeView1.DoDragDrop(e.Item, DragDropEffects.Move)
End Sub Private Sub ListBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox1.DragDrop
If e.Data.GetDataPresent(GetType(System.Windows.Forms.TreeNode)) Then
Dim dragNode As TreeNode = e.Data.GetData(GetType(System.Windows.Forms.TreeNode))
Call AddToListBox(dragNode)
End If
End Sub Private Sub ListBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox1.DragEnter
e.Effect = DragDropEffects.All
End Sub Private Sub AddToListBox(ByVal Node As TreeNode)
Me.ListBox1.Items.Add(Node.Text)
For Each item As TreeNode In Node.Nodes
Call AddToListBox(item)
Next
End Sub
End Class
Public Class Form2 Private Sub TreeView1_ItemDrag(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemDragEventArgs) Handles TreeView1.ItemDrag
TreeView1.DoDragDrop(e.Item, DragDropEffects.Move)
End Sub Private Sub ListBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox1.DragDrop
If e.Data.GetDataPresent(GetType(System.Windows.Forms.TreeNode)) Then
Dim dragNode As TreeNode = e.Data.GetData(GetType(System.Windows.Forms.TreeNode))
Call AddToListBox(dragNode)
End If
End Sub Private Sub ListBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox1.DragEnter
e.Effect = DragDropEffects.All
End Sub Private Sub AddToListBox(ByVal Node As TreeNode)
Me.ListBox1.Items.Add(Node.Text)
For Each item As TreeNode In Node.Nodes
Call AddToListBox(item)
Next
End Sub
End Class
Me.ListBox1.Items.Add(Node.Text)
For Each item As TreeNode In Node.Nodes
Call AddToListBox(item)
Next
End Sub 楼上这段代码,我不明白,
怎么还在过程里调用自身,这是死循环了吗?
{
for (int i = 0; treeview.GetselectedNodesText.Count > i; i++)
{
listBox1.Items.Add(treeview.GetselectedNodesText[i].ToString());
}
} private void listBox1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(TreeNode)))
e.Effect = DragDropEffects.Move;
else
e.Effect = DragDropEffects.None;
} private void treeview_ItemDrag(object sender, ItemDragEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
DoDragDrop(e.Item, DragDropEffects.Move | DragDropEffects.Copy);
}
}