Click here to Skip to main content
15,888,337 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
in my code i am trying to draw polygon i need to find coordinate points of polygon and display that coordinate points in to text Boxes .can anybody help me to figure it out ...

C#
List<point> points = new List<point>();
         private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
       {
           //points.Clear();
           // points.Push(e.Location);

           if (e.Button == MouseButtons.Left)
           {
               points.Add(e.Location);

               pictureBox1.Invalidate();
           }


          private void pictureBox1_Paint(object sender, PaintEventArgs e)
            {
           if (points.Count > 1)
               e.Graphics.DrawPolygon(Pens.DarkBlue, points.ToArray());

           foreach (Point p in points)
           {
               e.Graphics.FillEllipse(Brushes.Red,
                                      new Rectangle(p.X - 2, p.Y - 2, 4, 4));
           }
           }</point></point>
Posted
Updated 2-May-14 22:14pm
v2
Comments
Mitchell J. 3-May-14 3:25am    
What have you tried yourself that isn't working? Is there anything you might have tried already but haven't because you did't think it would work?

I'm asking this because the solution, if I gave it to you, would actually be really, really simple...
Member 10785117 3-May-14 3:49am    
with above code i am drawing polygon on mouse click with highlighting the coordinate points but i don't know how to get coordinate points with respect to picture box if you have any answer please help me
Member 12029366 3-Oct-15 0:38am    
Interesting comments . my colleagues last month came across http://pdf.ac/33PGYk to export pdf , It's kind of effortless to navigate and it's convenient - I saw on the website they are offering a free promotion ongoing

You have your coordinates already: they are in the points collection.
So all you need to do is convert each Point to a string, and display it - but I wouldn't use TextBoxes, because there are a variable number of Points, depending on how many times the user clicks - and if each one needs a textbox... yuck.

A better solution is to use a single multiline textbox, or a datagridview. The later is simple:
C#
myDdataGridView.DataSource = null;
myDataGridView.DataSource = points;
The system will sort out the display for you. (You need the first line to "refresh" the DataGridView)
 
Share this answer
 
Comments
Member 10785117 3-May-14 4:28am    
on mouse click i am getting coordinates points in to datagrid view twice for a single click ...
OriginalGriff 3-May-14 4:39am    
Strange: I just tried with your code and it worked fine: red dots, connected by black lines, and one point per click.
List<Point> points = new List<Point>();
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
points.Add(e.Location);

pictureBox1.Invalidate();
dataGridView1.DataSource = null;
dataGridView1.DataSource = points;
}
}

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (points.Count > 1)
e.Graphics.DrawPolygon(Pens.DarkBlue, points.ToArray());

foreach (Point p in points)
{
e.Graphics.FillEllipse(Brushes.Red,
new Rectangle(p.X - 2, p.Y - 2, 4, 4));
}
}
So...Have you checked that there is nowhere else you add items to the Points list?
Member 10785117 3-May-14 5:28am    
ya i checked out with what you wrote here ,but i am getting coordinates point twice in a single click in datagridview ..how can i resolve it ?<br>
OriginalGriff 3-May-14 5:53am    
Do you mean you get an X and a Y in separate columns in the view?
Or that you get x=2, y=5 repeated?
Member 10785117 3-May-14 5:57am    
x=2,y=5 these coordinates are repeated twice in two rows....on every click i am getting twice
The solution is so simple because you have practically written the solution in that code snippet already, unless I am missing something that you haven't explained.

Here, where you did this:
C#
foreach (Point p in points)
{
   e.Graphics.FillEllipse(Brushes.Red,
   new Rectangle(p.X - 2, p.Y - 2, 4, 4));
}


Can't you just create a function called UpdateTextbox(), like this?
C#
void UpdateTextbox()
{
   exampletextbox.Text = ""; 
   foreach (Point p in points)
   {
      exampletextbox.Text += p.ToString();
   }
}

You can customize the way that you represent the coodinates, but really this code is not that different from what you have already. points is defined globally, so you can use it in this function. All you need to do is call UpdateTextbox() at an appropriate moment, say in pictureBox1_MouseDown.
 
Share this 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