Click here to Skip to main content
15,910,121 members
Please Sign up or sign in to vote.
1.33/5 (2 votes)
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.
C#
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;
           }
        }



       }

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!


Pictures:
<img src="http://i61.tinypic.com/n2io08.jpg" border="0" alt="Image and video hosting by TinyPic">

<img src="http://i59.tinypic.com/34hurly.jpg" border="0" alt="Image and video hosting by TinyPic">
Posted
Comments
Sergey Alexandrovich Kryukov 26-Mar-14 19:22pm    
"the points do not appear..." Hm. Why do you think they should appear? Point is not a visible object...
What would it possibly mean, "draw two points". You cannot "draw a point". Just change a color of one pixel, or what? Even that does not have to be visible...
—SA
Member 10314038 26-Mar-14 19:25pm    
I want to draw ellipses on these points, that's my idea
Sergey Alexandrovich Kryukov 26-Mar-14 19:29pm    
Assuming your method Form1_Paint is the handler of the Paint event, you are not drawing those ellipses. You only draw them on Mouse down events, but who is going to persist it?
—SA

It looks like you don't understand the concept of graphic rendering. Please see my most recent comment. Any invalidation will wipe out anything not supported by drawing in the handler of the Paint event (or, optionally, overridden method Control.OnPaint.

Try to understand rendering. My past answers can help you:
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
capture the drawing on a panel[^],
How to speed up my vb.net application?[^],
Drawing Lines between mdi child forms[^].

—SA
 
Share this answer
 
v3
Comments
Member 10314038 26-Mar-14 19:44pm    
Thank you, I will check them and write tomorrow if I solved the problem. Thanks again
Sergey Alexandrovich Kryukov 26-Mar-14 20:20pm    
Great.
—SA
Graphics gr;
public Form1()
{
InitializeComponent();
}
private Point p1, p2;
List<point> p1List = new List<point>();
List<point> p2List = new List<point>();


private void Form1_Paint_1( object sender, PaintEventArgs e )
{



}
private void Form1_MouseDown_1(object sender, MouseEventArgs e)
{

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
{
C#
<pre lang="c#">

p2.X = e.X;
p2.Y = e.Y;

p1List.Add(p1);
p2List.Add(p2);


using (var p = new Pen(Color.Blue, 4))
{
for (int i = 0; i < p1List.Count; i++)
{
gr.DrawLine(p, p1List[i], p2List[i]);
}
}
p1.X = 0;
}
}

this might help you
 
Share this answer
 
v2
Comments
Member 10314038 27-Mar-14 20:45pm    
Thanks it worked, but now can u please help me with the other problem?
Mohammed Nabeel Khan 28-May-14 19:41pm    
you may rate my answer :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900