spspli
spspli

Reputation: 3338

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?

Upvotes: 8

Views: 12051

Answers (3)

Austin
Austin

Reputation: 1

This is actually quite simple. You can use the ShapeContainer class just as you would a Canvas. Set the parent of the LineShape to the ShapeContainer and the parent of the ShapeContainer to the Form.

Upvotes: 0

user153923
user153923

Reputation:

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

Upvotes: 3

Larry
Larry

Reputation: 18031

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.

Upvotes: 6

Related Questions