Hello guys,
My problem is that when I want to draw 2 points on the form and connect them, the points do not appear but the connections are still on the form. I am using DrawEllipse and DrawLine methods.
On the first picture you can see my first click on the form it draws a point and on the second picture the connection is drawn but without the points.
I need your help please!
My problem is that when I want to draw 2 points on the form and connect them, the points do not appear but the connections are still on the form. I am using DrawEllipse and DrawLine methods.
Code:
public partial class Form1 : Form
{
private Point p1, p2;
List<Point> p1List = new List<Point>();
List<Point> p2List = new List<Point>();
public Form1()
{
InitializeComponent();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
using (var p = new Pen(Color.Blue, 4))
{
for (int x = 0; x < p1List.Count; x++)
{
e.Graphics.DrawLine(p, p1List[x], p2List[x]);
}
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
Graphics gr = this.CreateGraphics();
int x = e.X;
int y = e.Y;
// Create pen.
Pen whitePen = new Pen(Color.Blue, 3);
Pen red = new Pen(Color.Red, 3);
SolidBrush whiteBrush = new SolidBrush(Color.Blue);
// Create rectangle for ellipse.
Rectangle rect = new Rectangle(x - 5, y - 5, 10, 10);
gr.DrawEllipse(whitePen, rect);
gr.FillEllipse(whiteBrush, rect);
if (p1.X == 0)
{
p1.X = e.X;
p1.Y = e.Y;
}
else
{
p2.X = e.X;
p2.Y = e.Y;
p1List.Add(p1);
p2List.Add(p2);
Invalidate();
p1.X = 0;
}
}
}
I need your help please!