我如何在拖放中传递列表对象? [英] How I pass a list object in a drag drop?

查看:59
本文介绍了我如何在拖放中传递列表对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在vb.net中没有很多编码拖放的经验。



下面代码的一些上下文。当我在其中进行选择时,我有一个treeview TVDatabase,节点被复制到第二个teeview(TreView2)。有一些逻辑用于标记所选节点并从第二个树视图中删除取消选择的节点。这为TVDatabase提供了多选择的行为。



我希望能够右键单击并拖动并获取有关所有选定节点的信息并将其放入另一个容器中,这将读取该数据并采取进一步的行动。我通过简单地弹出一些消息框来代理这个。



所以我写了一些代码,它们会在mousedown上触发并构建我想要传输的对象。这是每个选定节点上的标签列表(在这种情况下都是数据行对象)。



我在控件上写了一个dragover例程我想要放下,测试被拖动的数据是否是List(of Datarow)并显示相应的图标。



我在控件上编写了一个dragdrop例程,它应该检查数据是否为List(Datarow),然后在其中添加一个要使用的变量。 br />


这些潜艇不起作用。测试通过,但是dragdrop子句中的循环错误输出为DropList = nothing。我完全不相信我理解e.Data.GetData的语法。



任何人都可以告诉我我做错了什么?



I haven't had a lot of experience with coding drag drop in vb.net.

A little bit of context to the code below. I have a treeview TVDatabase when I make selections in it the nodes are copied to a second teeview (TreView2). There is some logic to mark selected nodes and remove deselected node from the second treeview. This give multi-select like behavior to TVDatabase.

I want to be able to right click and drag and take information about all selected nodes and drop them into another container, which will read that data and take some further actions. I've proxied that here by simply popping up some message boxes.

So I've written some code that fires on mousedown and builds the object I want to transfer. This is a list of the tags on each selected node (in this case all are datarow objects).

I've written a dragover routine on the control I want to drop on, to test if the data being dragged is a List(of Datarow) and show the appropriate icon.

I've written a dragdrop routine on the control which should check that the data is a List(of Datarow) then put in it a variable to be used.

These subs don't work. The test are passed, but the loop in the dragdrop sub errors out as DropList = nothing. I'm not at all confident I understand the syntax around e.Data.GetData.

Can anyone give me a steer about what am I doing wrong?

Private Sub TVDatabase_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) HandlesTVDatabase.MouseDown
        If e.Button = Windows.Forms.MouseButtons.Right Then
            Dim DropList As New List(Of DataRow)
 
            For Each node As TreeNode In TreeView2.Nodes
            	DropList.Add(node.Tag)
            Next
 
        	TVDatabase.DoDragDrop(DropList, DragDropEffects.All)
 
 
        End If
End Sub

Private Sub TextBox_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) HandlesTextBox1.DragOver
 
        If e.Data.GetDataPresent(GetType(List(Of DataRow))) Then
            	e.Effect = DragDropEffects.Move
            Else
            	e.Effect = DragDropEffects.None
            End If
 
End Sub
 
Private Sub TextBox_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)Handles TextBox1.DragDrop
        Dim DropList As New System.Collections.Generic.List(Of System.Data.DataRow)
        If e.Data.GetDataPresent(GetType(List(Of DataRow))) Then
        	DropList = CType(e.Data.GetData("System.Collections.Generic.List(Of System.Data.DataRow)"),List(Of DataRow))
 
   	     For Each row As DataRow In DropList
            	MsgBox(row(0) + " " + row(1) + " " + row(2) + " " + row(3))
            Next
 
        End If
 
End Sub

推荐答案

这篇关于我如何在拖放中传递列表对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆