By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,384 Members | 714 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,384 IT Pros & Developers. It's quick & easy.

Linking between Form Controls (Drag Events)

P: 179
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!
Dec 19 '08 #1
Share this Question
Share on Google+
6 Replies

P: 61
Handle the NodeMouseClick of treeview & form's mousemove events.
Few lines of code is shown below.You can enhance it.
Expand|Select|Wrap|Line Numbers
  1. private void treeView2_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
  2.         {
  3.             tracked = true;
  4.             start.X = treeView2.Location.X + e.Location.X;
  5.             start.Y = treeView2.Location.Y + e.Location.Y;
  6.         }
  7.         private void Form1_MouseMove(object sender, MouseEventArgs e)
  8.         {
  9.             if ((tracked == false) || (e.Button != MouseButtons.Left))
  10.             {
  11.                 return;
  12.             }
  13.             end = e.Location;
  14.             Pen pen = new Pen(Color.FromArgb(96, 155, 173), 1);
  15.             this.Refresh();
  16.             Graphics g = this.CreateGraphics();
  17.             Rectangle rc = new Rectangle(start, new Size(start.X - end.X, start.Y - end.Y));
  18.             g.DrawLine(pen, start, end);
  19.             System.Diagnostics.Trace.Write("in mousemove\n");
  20.             g.Dispose();
  21.         }
Dec 20 '08 #2

P: 179

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
Dec 22 '08 #3

P: 61
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.
Dec 23 '08 #4

P: 2
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 ...
May 21 '09 #5

P: 2
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.
May 21 '09 #6

P: 1

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
Jul 30 '09 #7

Post your reply

Sign in to post your reply or Sign up for a free account.