Linking between Form Controls (Drag Events)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • IanWright
    New Member
    • Jan 2008
    • 179

    Linking between Form Controls (Drag Events)

    I'm trying to test something to see if its functionally possible, and am having a little trouble with drag events...

    I basically want to draw a line between two TreeNode entities, contained within two seperate Treeviews.

    I can override the Paint method and draw a link between the last selected Node and the current mouse position (set on the Forms MouseMove), but this only works when I'm not dragging/dropping.

    The functionality I need, is MouseDown over a node, start dragging to other treeview, a line is drawn from the edge of the Treenode following the mouse, then when releasing the mouse on another treeview, this line attaches to it's bounding box too.

    Does anyone have any suggestions as to which Control's events I need to be using, as it's feeling that I can't quite do it in the way I expected...

    Thank you!
  • Ramk
    New Member
    • Nov 2008
    • 61

    #2
    Originally posted by IanWright
    I'm trying to test something to see if its functionally possible, and am having a little trouble with drag events...

    I basically want to draw a line between two TreeNode entities, contained within two seperate Treeviews.

    I can override the Paint method and draw a link between the last selected Node and the current mouse position (set on the Forms MouseMove), but this only works when I'm not dragging/dropping.

    The functionality I need, is MouseDown over a node, start dragging to other treeview, a line is drawn from the edge of the Treenode following the mouse, then when releasing the mouse on another treeview, this line attaches to it's bounding box too.

    Does anyone have any suggestions as to which Control's events I need to be using, as it's feeling that I can't quite do it in the way I expected...

    Thank you!
    Handle the NodeMouseClick of treeview & form's mousemove events.
    Few lines of code is shown below.You can enhance it.
    Code:
    private void treeView2_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
            {
                tracked = true;
                start.X = treeView2.Location.X + e.Location.X;
                start.Y = treeView2.Location.Y + e.Location.Y;
            }
            private void Form1_MouseMove(object sender, MouseEventArgs e)
            {
                if ((tracked == false) || (e.Button != MouseButtons.Left))
                {
                    return;
                }
                end = e.Location;
                Pen pen = new Pen(Color.FromArgb(96, 155, 173), 1);
                this.Refresh();
                Graphics g = this.CreateGraphics();
                Rectangle rc = new Rectangle(start, new Size(start.X - end.X, start.Y - end.Y));
                g.DrawLine(pen, start, end);
                System.Diagnostics.Trace.Write("in mousemove\n");
                g.Dispose();
            }

    Comment

    • IanWright
      New Member
      • Jan 2008
      • 179

      #3
      Ramk,

      Thanks for the reply. I actually believe that was the same I was doing before but the MouseMove event does not get thrown on the form while 'Dragging'... or at least it didn't seem to in my case.

      Only thing I was really doing differently was I was causing Invalidate... as I never actually knew about the CreateGraphics method.

      I'm guessing also that Rectangle was a mistake, seeing as you're not using it :P

      Comment

      • Ramk
        New Member
        • Nov 2008
        • 61

        #4
        Originally posted by IanWright
        Ramk,

        Thanks for the reply. I actually believe that was the same I was doing before but the MouseMove event does not get thrown on the form while 'Dragging'... or at least it didn't seem to in my case.

        Only thing I was really doing differently was I was causing Invalidate... as I never actually knew about the CreateGraphics method.

        I'm guessing also that Rectangle was a mistake, seeing as you're not using it :P
        Did you check the mousemove event is attached to the form? Other case could be, a panel could have occupied the entire form, in which the mouse move messages will go to the panel when the mouse is over it.

        CreateGraphics will create the graphcis object for us, through which we can draw on the screen irrespective of the underlying graphics hardware.

        Yes, the rectangle is unnecessary. I want to comment it by editing my post, but thought, it won't harm much & kept as it is.

        Comment

        • merrkee
          New Member
          • May 2009
          • 2

          #5
          Originally posted by IanWright
          I'm trying to test something to see if its functionally possible, and am having a little trouble with drag events...

          I basically want to draw a line between two TreeNode entities, contained within two seperate Treeviews.

          I can override the Paint method and draw a link between the last selected Node and the current mouse position (set on the Forms MouseMove), but this only works when I'm not dragging/dropping.

          The functionality I need, is MouseDown over a node, start dragging to other treeview, a line is drawn from the edge of the Treenode following the mouse, then when releasing the mouse on another treeview, this line attaches to it's bounding box too.

          Does anyone have any suggestions as to which Control's events I need to be using, as it's feeling that I can't quite do it in the way I expected...

          Thank you!
          I've run into this same problem. To restate what you've already said: if you check the MouseMove event normally over multiple controls, the event is thrown for each particlar control. However, if you MouseDown over one control, drag the mouse over other controls, the MouseMove event is thrown but the object throwing it is the original control that contained the MouseDown event.

          I'm trying to drag information from one control to the next but no MouseMove event is thrown for the next control ...

          I could hack around it but would prefer a more straightforward solution.

          I'll post it if I find it ...

          Comment

          • merrkee
            New Member
            • May 2009
            • 2

            #6
            Found a solution ...

            I needed to disable the drag drop feature. I had thought of this previously and the method to do this is AllowDrop but when you try to set AllowDrop my compiler tells me I can only "get" and not "set" AllowDrop.

            Well apparently, you can't set the AllowDrop of your base Form or UserControl, but you can set AllowDrop of controls you've added to your base Form or UserControl.

            this->controlName->AllowDrop = false;

            Now all the mouse events are raised while holding your mouse buttons down ...

            BTW, my compiler is Visual C++ .NET 2005.

            Comment

            • umaram
              New Member
              • Jul 2009
              • 1

              #7
              Need Help

              Hi,

              I have a requirement to draw between to treeview in a form in c#. Could you please give the code sample to proceed?

              Thanks in advance

              Comment

              Working...