As @gbianchi says in a comment, the objects that are drawn by
Graphics are not "clickables", so they do not have a
Click event to subscribe to.
I will give you a solution using mathematics. The first thing we should know is that the equation of a line is defined with this formula:
y = mx + b
Knowing this, what you must do is store in some variable the two points that define your line. To simplify everything, I will work with integers only.
Following your example, I see that you draw your line from
n1.centro , so these are the points that define your line.
Now, what we do is subscribe to the Click event from where you are drawing (
PictureBox ..). In this example, to the form. In this event, we first calculate the equation of the line given two points, and then we check if the point where it was pressed corresponds to the defined line:
private void form1_MouseClick(object sender, MouseEventArgs e)
Point p1 = n.centro;
int sensibilidad = 5;
//Calculamos la ecuación de la recta dados dos puntos
int m = (p2.Y - p1.Y) / (p2.X - p1.X);
var b = p1.Y - (m * p1.X);
//Comprobamos el valor de y en la recta para el valor X del punto pulsado
int y = m * e.Location.X + b;
//Si la y está dentro de la línea (con un margen de sensibilidad que decidamos)
if (e.Location.Y>=y-sensibilidad && e.Location.Y<=y+sensibilidad)
//estás en la linea
MessageBox.Show("Estás en la línea");
As the line is a very fine object, we define a "sensitivity" to make it easier to detect the click.