Make line clickable VB.NET

Hi there, i am drawing some lines on a System.Drawing.Graphics object.

for example
gMyGraphics.DrawLine(pMyPen, 0 + lineStartMargin, lineYpos, lineWidth + lineStartMargin, lineYpos)

Open in new window


I want that line to be clickable and run a function when its clicked.
Any ideas?
ioustinos_sarrisAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CodeCruiserCommented:
You have two options

1) Keep the points at which the line exists and when mouse is clicked within the container of the line, get the coordinates and if the coordinates match with any of those of the line then execute the function you want.

2) Use a control such as label instead of drawing a line. This will obviously work only if you want to draw a straight horizontal line.
Mohamed AbowardaSenior Software EngineerCommented:
You can use panel or picture box and set the BackgroundImage property to a line you have drawn in image file then handle the panel or picture box Click event.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics TeacherCommented:
You can calculate the distance between the mouse click and the line and consider it a hit if it is less than a certain threshold.  I recommend somewhere around 5 to 7 pixels.

So the mouse click coords would be (Px, Py) and the endpoints of the line would be (Ax, Ay) and (Bx, By):
Private Function PointToLineDist( _
            ByVal Px As Single, ByVal Py As Single, _
            ByVal Ax As Single, ByVal Ay As Single, _
            ByVal Bx As Single, ByVal By As Single) As Single

        Dim q As Single

        If (Ax = Bx) And (Ay = By) Then
            ' A and B passed in define a point, not a line.
            ' Point to Point Distance
            Return PointToPointDist(Px, Py, Ax, Ay)
        Else
            ' Distance is the length of the line needed to connect the point to
            ' the(segment)such that the two lines would be perpendicular.

            ' q is the parameterized value needed to get to the intersection
            q = ((Px - Ax) * (Bx - Ax) + (Py - Ay) * (By - Ay)) / _
                ((Bx - Ax) * (Bx - Ax) + (By - Ay) * (By - Ay))

            ' Limit q to 0 <= q <= 1
            ' If q is outside this range then the Point is somewhere past the 
            ' endpoints of our segment.  By setting q = 0 or q = 1 we are 
            ' measuring the actual distacne from the point to one of the 
            ' endpoints(instead)
            If q < 0 Then q = 0
            If q > 1 Then q = 1

            ' Distance
            Return PointToPointDist( _
                Px, Py, (1 - q) * Ax + q * Bx, (1 - q) * Ay + q * By)
        End If
    End Function

    Private Function PointToPointDist(ByVal Ax As Single, ByVal Ay As Single, _
                                   ByVal Bx As Single, ByVal By As Single) As Single
        ' PointToPointDist = SquareRoot((Bx - Ax)^2 + (By - Ay)^2)
        Return Math.Sqrt((Bx - Ax) * (Bx - Ax) + (By - Ay) * (By - Ay))
    End Function

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.