Search code examples
c#winformspolygonsystem.drawing

Detecting mouse click on the node of polygone


I am creating a WindowsForms application. In this application I need to draw polygone, using the array with coordinates of polygone's vertexes(nodes).

When user click on one of polygone's nodes, I need to detect, which node is chosen, to suggest user to delete this node.

I am using WindowsForms, PictureBox and Graphics.DrawPolygon for drowing polygones.

How can I detect, if user have clicked on one of nodes? Are there more suitable ways to draw polygones, than WindowsForm and PictureBox?

Thanks in advance.


Solution

  • You would have to check when the picturebox is clicked by running through the nodes points and testing if they are where the user has clicked.

    Point[] listOfNodes = new Point[1];
    private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
    {
        foreach (Point item in listOfNodes)
        {
            if (item == e.Location)
            {
                //The node was clicked.
            }
        }
    }