# Visual Basic > Visual Basic .NET >  Move drawn shapes

## amin.grf

Hi, This's my first time here, thank you for this wonderful forum.
Recently been working on a paint like program I've download it from vp-helper.
The program has same basing drawing tools like line, rectangular, ellipse, star...etc
- I'm trying to add a free hand drawing tool by mouse, I did that it works just fine.
---------
The problem is when I try to select the Path to move it by mouse It won't get selected, only the
 drag handler squares are moving alone not with the shape. see the attached image and "DrawableLine" class code, any help is much appreciated.





```
Imports System.Math
Imports System.Xml.Serialization
Imports System.Drawing.Drawing2D
Imports System.Collections.Generic

<Serializable()> _
Public Class DrawableLine
    Inherits Drawable

    ' Constructors.
    Public Sub New()
    End Sub
    Private line As New List(Of Point)
    Public Sub New(ByVal fore_color As Color, Optional ByVal line_width As Integer = 0, Optional ByVal new_x1 As Integer = 0, Optional ByVal new_y1 As Integer = 0, Optional ByVal new_x2 As Integer = 1, Optional ByVal new_y2 As Integer = 1)
        MyBase.New(fore_color, Nothing, line_width)

        X1 = new_x1
        Y1 = new_y1
        X2 = new_x2
        Y2 = new_y2

        'start the line:
        Dim Point1 As New Point(X1, Y1)
        line = New List(Of Point)
        'line.Add(Point1)
        line.Add(Point1)

    End Sub
 
    ' Draw the object on this Graphics surface.
    Public Overrides Sub Draw(ByVal gr As System.Drawing.Graphics)
        If IsSelected Then
            ' Draw the line highlighted.
            Dim highlight_pen As New Pen(Color.Yellow, LineWidth)
            gr.DrawLines(highlight_pen, line.ToArray)
            highlight_pen.Dispose()

            ' Draw grab handles.
            DrawGrabHandle(gr, X1, Y1)
            DrawGrabHandle(gr, X2, Y2)
        Else
            If line.Count < 2 Then Return
            ' Just draw the line.
            Dim fg_pen As New Pen(ForeColor, LineWidth)
            'Dim bigArrow As New AdjustableArrowCap(3, 3)
            'fg_pen.CustomEndCap = bigArrow
            gr.DrawLines(fg_pen, line.ToArray)
            fg_pen.Dispose()
        End If
    End Sub

    ' Return the object's bounding rectangle.
    Public Overrides Function GetBounds() As System.Drawing.Rectangle
        Return New Rectangle( _
            Min(X1, X2), _
            Min(Y1, Y2), _
            Abs(X2 - X1), _
            Abs(Y2 - Y1))
    End Function
    ' Move the second point.
    Public Overrides Sub NewPoint(ByVal x As Integer, ByVal y As Integer)
        X2 = x
        Y2 = y
        'start the line:
        Dim Point1 As New Point(x, y)
        line.Add(Point1)

    End Sub
    ' Return True if this point is on the object.
    Public Overrides Function IsAt(ByVal x As Integer, ByVal y As Integer) As Boolean
        Return PointNearSegment(x, y, X1, Y1, X2, Y2)
    End Function



    ' Return True if the object is empty (e.g. a zero-length line).
    Public Overrides Function IsEmpty() As Boolean
        Return (X1 = X2) AndAlso (Y1 = Y2)
    End Function
End Class
```

----------


## Shaggy Hiker

You might want to look into the GraphicsPath. As it stands, you are using the bounding rectangle, which is a bit risky. What happens if two rectangles overlap partially, or completely? If you do drawings the GraphicsPath, you can test whether a click is on the line itself, or inside the object, without needing to deal with the imprecision inherent in bounding rectangles.

Of course, if you went that way, your problem would also go away, or at least become something totally different, since it would be a totally different solution to the problem.

I've used GraphicsPath objects to draw a flow diagram kind of thing where the lines had to respond to the mouse hovering over them (a tooltip), or clicking on them (more than a tool tip). I'd say that it's just a better way to go for drawing.

----------


## amin.grf

thanks Shaggy for replying, this code is written by vp-helper Im only modifying it. hover I did test different overlapping shapes, it work just fine, 



I think I need to transform the moving points of mouse to the bounding rectangle 



```
' Return the object's bounding rectangle.
    Public Overrides Function GetBounds() As System.Drawing.Rectangle
        Return New Rectangle( _
            Min(X1, X2), _
            Min(Y1, Y2), _
            Abs(X2 - X1), _
            Abs(Y2 - Y1))
    End Function
```

just like this star Shape class like this: 



```
        ' Transform to the bounding rectangle.
        Dim x_scale As Double = Abs(X2 - X1) / 2
        Dim y_scale As Double = Abs(Y2 - Y1) / 2
        Dim dx As Double = (X2 + X1) / 2
        Dim dy As Double = (Y2 + Y1) / 2
        For i As Integer = 0 To 9
            pts(i).X = CSng(pts(i).X * x_scale + dx)
            pts(i).Y = CSng(pts(i).Y * y_scale + dy)
        Next i

        Return pts
```

----------


## jmcilhinney

You might like to check out this CodeBank thread of mine:

https://www.vbforums.com/showthread....g-GDI-Drawings

Note that someone else built on what I did later in the thread.

----------

