Reputation: 49
I try to draw a line between one big button and all the small buttons, but everytime i click the big button to spawn a new smaller button, the line gets replaced so it only has one line and it's between the latest small button and the big button, how do i fix this?
Here is my code:
Pen blackPen = new Pen(Color.Black, 1);
float x1 = btn1.Location.X;
float y1 = btn1.Location.Y;
foreach (Control control in this.Controls)
{
if (control is Button)
{
float x2 = x - 100;
float y2 = y;
PointF point1 = new PointF(x1, y1);
PointF point2 = new PointF(x2, y2);
e.Graphics.DrawLine(blackPen, point1, point2);
}
}
Upvotes: 1
Views: 47
Reputation: 340
I have a feeling that the x and y you are giving are actually control properties. At least they are not defined anywhere in code you give to us, so you are likely drawing just multiple lines ending to the same place. Try following:
if (control is Button)
{
float x2 = control.x - 100;
float y2 = control.y;
PointF point1 = new PointF(x1, y1);
PointF point2 = new PointF(x2, y2);
e.Graphics.DrawLine(blackPen, point1, point2);
}
Upvotes: 1
Reputation: 474
This is by design you need to redraw the line on invalidated areas. Try to drag the window off screen to invalidate the whole window and any lines should just disappear.
You must hook into the OnPaint()
event and draw the lines again every time it's needed. You can store the lines you want to draw by keeping a list of point pairs.
Upvotes: 0