TreeView DragDrop [英] TreeView DragDrop
问题描述
大家好!
我感到非常有趣,因为这是我第一次在论坛中提问.我要尽可能清楚地描述我的问题,因为我的英语不好.
我正在尝试将拖放"从一个树形视图编程到另一个树形视图.我知道它非常易于管理,但是我想让它看起来像是您确实从树状视图中拖动了节点文本.
这是我到目前为止所做的:
我创建了带有标签和透明键的Windows窗体,以便仅标签可见.我已将TopMost表单设置为True,并在应进行拖动的主表单中声明了它.
在应从其拖动名称的树视图的mousedown事件中,我将移动表单重新定位到treenode并给出了treenode的文本.
代码如下所示:
Hi guys!
I feel very funny because this is the first time that i''m asking a question in a forum. I''m gonna try to describe my problem as clear as possible cuz'' i''m not that good with english.
I''m trying to program a ''drag and drop'' from one treeview to another. I know it''s very easy to manage it but i want to make it look like u really drag the node text from the tree view.
This is what i did so far:
I created a windows form with a label and with a transparency key so that only the label will be visible. I have set the form TopMost to True and declared it in the main form where I should do the drag.
On the mousedown event of the treeview from which the name should be dragged, i relocated the moving form to the treenode and gave the text of the treenode.
The code looks like this:
Private Sub treeFrom_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles treeFrom.MouseDown
Dim name As String
Dim key As String
If Not treeFrom.GetNodeAt(e.Location) Is Nothing Then
key = treeFrom.GetNodeAt(e.Location).Tag
name = treeFrom.Nodes(key).Text
frm.SetTheLabelName(name)
frm.Location = New Point(Me.Location.X + sender.Location.X + e.Location.X, Me.Location.Y + sender.Location.Y + e.Location.Y)
blnDrag = True
End If
End Sub
表单的新位置仍不完美,但我会对其进行管理,以便表单的标签文本恰好位于treenode文本上方.
这是treenode的mousemove事件:
The new location of the form is still not perfect but i will manage it so the label text of the form will fit exactly above the treenode text.
This is the mousemove event of the treenode:
Private Sub treeFrom_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles treeFrom.MouseMove
If e.Button = Windows.Forms.MouseButtons.Left Then
frm.Location = New Point(Me.Location.X + treeFrom.Location.X + e.Location.X, _
Me.Location.Y + treeFrom.Location.Y + e.Location.Y)
End If
End Sub
我还设法通过mousemove form事件将其移至表单上,但是问题是我必须为每个控件的MouseMove事件进行编程.
有什么方法只能用窗体MouseMove事件来管理它,因为当我捕获从一个事件到另一个事件的移动窗体时,标签从一个位置跳到另一个位置,并且我希望它平稳地进行过渡.一个很好的方法是直接从TreeFrom_MouseDown事件跳转到移动窗体的MouseMove事件,但是我不知道这是否是possilbe.
I also managed it to move over the form with the mousemove form event but the problem is that i have to program it for MouseMove event of each control.
Is there any way to manage it only with the form MouseMove event because when i catch the moving form from one event to other the label jump from one location to the other, and i want it to do the transition smoothly. A great way would be jumping directly from the TreeFrom_MouseDown event to the MouseMove event of the moving form but I don''t know if that is possilbe somehow.
推荐答案
您需要捕获"鼠标,以便将所有鼠标移动事件发送到控件处理程序.这是一个简单的例子.
You need to "capture" the mouse so all mouse move events are sent to your controls handler. Here''s a simple example.
private void treeView1_MouseDown(object sender, MouseEventArgs e)
{
treeView1.Capture = true;
_dragging = true;
}
private void treeView1_MouseMove(object sender, MouseEventArgs e)
{
if (_dragging)
{
label1.Text = "Tree: " + e.X.ToString() + ", " + e.Y.ToString();
}
}
private void treeView1_MouseUp(object sender, MouseEventArgs e)
{
treeView1.Capture = false;
_dragging = false;
}
注意:X和Y位置将相对于控制窗口.
Note: X and Y positions will be relative to the control window.
Is that what you''re looking for?
史蒂夫(Stnx),我想用这个解决方案笑了大约半个小时:laugh:.我简直不敢相信自己是MCAD,从未使用过Capture.
我尝试了很多复杂的事情,却不知道可以使用它,它用一行替换了我的30行代码.
再次thnx:-D,到目前为止,您是我最好的代码项目伙伴;大声笑
Hey Steve, thnx for the solution, I was laughing with it for about half an hour :laugh: . I can''t believe I''m an MCAD and never used Capture.
I tried a lot of complicated things not knowing that I could use that, it replaces my 30 lines of code with a single row.
Thnx again :-D , You''re my best codeproject buddy so far ;P lol
这篇关于TreeView DragDrop的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!