Question

So I am using Visual Basic Power Packs to do some basic easy graphics. I have the ability to draw many lines where I need to, and also VB power packs allows me to select the actual lines I have drawn, but I don't know how to implement code for when I do actually select these lines.

Here is my code:

Imports Microsoft.VisualBasic.PowerPacks

Public Class Form1

    Dim ptA, ptB As Point                     ' starting and ending point
    Dim down = False
    Dim lines As New List(Of LineShape)
    Dim temp As LineShape                     ' temporary line to be drawn
    Dim canvas As New ShapeContainer          'shape container 

    Private Sub Form1_MouseDown(sender As Object, e As MouseEventArgs) Handles MyBase.MouseDown
        down = True
        canvas.Parent = Me
        temp = New LineShape
        temp.Parent = canvas
        ptA = New Point(e.X, e.Y)
        temp.StartPoint = ptA
        temp.EndPoint = ptA
    End Sub

    Private Sub Form1_MouseUp(sender As Object, e As MouseEventArgs) Handles MyBase.MouseUp
        down = False
        ptB = New Point(e.X, e.Y)
        lines.Add(temp)
        temp = Nothing
    End Sub

    Private Sub Form1_MouseMove(sender As Object, e As MouseEventArgs) Handles MyBase.MouseMove
        If down = True Then
            temp.X2 = e.X
            temp.Y2 = e.Y
        End If
    End Sub

End Class

When I run and compile this, everytime I hold the mouse button down, move and release, I can draw a line. I can select the lines, I just don't know how to add code so that when I select it, it will do something. If someone could please help me I would greatly appreciate it. If someone could maybe just show me how to make a message box show up when a line is clicked with its starting and ending points.

I am creating a structural analysis program should allow a user to draw a building frame, then click on the lines and add properties such as the material it is made of and such.

Thank you very much!!

JD

Was it helpful?

Solution

Add a click handler to your temp Line...

Imports Microsoft.VisualBasic.PowerPacks

Public Class Form1

    Dim ptA, ptB As Point                     ' starting and ending point
    Dim down = False
    Dim lines As New List(Of LineShape)
    Dim temp As LineShape                     ' temporary line to be drawn
    Dim canvas As New ShapeContainer          'shape container 

    Private Sub Form1_MouseDown(sender As Object, e As MouseEventArgs) Handles MyBase.MouseDown
        down = True
        canvas.Parent = Me
        temp = New LineShape
        temp.Parent = canvas
        ptA = New Point(e.X, e.Y)
        temp.StartPoint = ptA
        temp.EndPoint = ptA
    End Sub

    Private Sub Form1_MouseUp(sender As Object, e As MouseEventArgs) Handles MyBase.MouseUp
        down = False
        ptB = New Point(e.X, e.Y)

        AddHandler temp.Click, AddressOf LineClickHandler

        lines.Add(temp)
        temp = Nothing
    End Sub

    Private Sub Form1_MouseMove(sender As Object, e As MouseEventArgs) Handles MyBase.MouseMove
        If down = True Then
            temp.X2 = e.X
            temp.Y2 = e.Y
        End If
    End Sub

    Private Sub LineClickHandler(sender As Object, e As MouseEventArgs)
        Dim MyLine As LineShape = DirectCast(sender, LineShape)

        MsgBox("Start = " & MyLine.StartPoint.ToString & " End Point = " & MyLine.EndPoint.ToString)
    End Sub

End Class
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top