Click here to Skip to main content
15,884,425 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
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


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:

VB
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


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.
Posted

You need to "capture" the mouse so all mouse move events are sent to your controls handler. Here's a simple example.

C#
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;
}


Note: X and Y positions will be relative to the control window.

Is that what you're looking for?
 
Share this answer
 
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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900