Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw straight line between two controls in a user control?

I have a usercontrol named groupControl: It has two flowlayoutpanel - source panel and destination panel. It also has a button.

I have another usercontrol named item. I will dynamically lay N item controls in source panel and M item controls in target panel.

I want there are straight lines between each item control and button on groupControl.

Finally I have a test form MainForm, it also contains a flowlayoutpanel. I will dynamically lay X groupcontrols on MainForm.

How could I draw straight lines between each usercontrol item and the button on the same groupControl?

like image 756
spspli Avatar asked Apr 05 '11 19:04

spspli


2 Answers

The Visual Basic Power Pack contains a DataRepeater, and some shapes (oval, rectangle..) including a line. See this link.

It is called "Visual Basic" Power Pack, but it can be used in a C# project without any hassle.

Look at the DataRepeater, not only it will help you to fill your panel with custom controls as items, but it contains what you need to put a line between them.

like image 195
Larry Avatar answered Sep 30 '22 06:09

Larry


You'd have to edit something like this to fit your correct Start and End Points (pt1 and pt2), but...

FlowLayoutPanel flowLayoutPanel1;
FlowLayoutPanel flowLayoutPanel2;

private void ShippingForm_Paint(object sender, PaintEventArgs e) {
  using (Graphics g = e.Graphics) {
    Point pt1 = flowLayoutPanel1.Location;
    Point pt2 = flowLayoutPanel2.Location;
    using (Pen p = new Pen(Brushes.Black)) {
      g.DrawLine(p, pt1, pt2);
    }
  }
}

EDIT:

If you have a form called ShippingForm (like I did above), go to the form's event handlers in the GUI and add double click on the Pant event to generate the empty method stub. VS's GUI of Form

like image 20
jp2code Avatar answered Sep 30 '22 05:09

jp2code