How to Erase a Line created using .DrawLine

raklali 236 Reputation points
2022-01-16T08:28:28.797+00:00

On a PictureBox I have drawn Several ellipses and lines. One Line originates (X1,Y1) from centre of PictureBox1 and extends till certain range (length-X2,Y2). Using a Slider/HScrollBar I want to redraw this line by assgining New Values to the X2, Y2 and also erase the line drawn using Old/ original X2,Y2.

The Old Line remains partially visible. On refreshing or invalidating the PictureBox1, all other drawn objects disaappear.

Can someone help?

Developer technologies | VB
{count} votes

Accepted answer
  1. Castorix31 90,686 Reputation points
    2022-01-16T10:18:11.78+00:00

    From comments, the test code I did, with a similar method as LesHay-2099 described, with a List of lines (Segment Class) :
    (PictureBox and Scrollbars added from Design Mode)

    Public Class Form1
    
        'Method from http://csharphelper.com/blog/2018/12/let-the-user-draw-lines-in-c/
    
        Private Segments As List(Of Segment) = New List(Of Segment)()
        Private NewSegment As Segment = Nothing
    
        Private SpecialSegment As Segment = Nothing
        Private ptOrigin As Point = Nothing
        Private ptDest As Point = Nothing
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ptOrigin = New Point(CInt(PictureBox1.ClientSize.Width / 2), CInt(PictureBox1.ClientSize.Height / 2))
            ptDest = New Point(CInt(PictureBox1.ClientSize.Width / 2), CInt(PictureBox1.ClientSize.Height / 2))
            SpecialSegment = New Segment(Pens.Red, ptOrigin, ptDest)
            Segments.Add(SpecialSegment)
            HScrollBar1.Minimum = -CInt(PictureBox1.ClientSize.Width / 2)
            HScrollBar1.Maximum = CInt(PictureBox1.ClientSize.Width / 2) + HScrollBar1.LargeChange - 1
            HScrollBar2.Minimum = -CInt(PictureBox1.ClientSize.Height / 2)
            HScrollBar2.Maximum = CInt(PictureBox1.ClientSize.Height / 2) + HScrollBar2.LargeChange - 1
        End Sub
    
        Private Sub PictureBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseDown
            NewSegment = New Segment(Pens.Blue, e.Location, e.Location)
            PictureBox1.Refresh()
        End Sub
    
        Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
            e.Graphics.Clear(PictureBox1.BackColor)
            e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
            For Each segment As Segment In Segments
                segment.Draw(e.Graphics)
            Next
            If NewSegment IsNot Nothing Then NewSegment.Draw(e.Graphics)
        End Sub
    
        Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove
            If NewSegment Is Nothing Then Return
            NewSegment.pt2 = e.Location
            PictureBox1.Refresh()
        End Sub
    
        Private Sub PictureBox1_MouseUp(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseUp
            If NewSegment Is Nothing Then Return
            NewSegment.pen1 = Pens.Black
            Segments.Add(NewSegment)
            NewSegment = Nothing
            PictureBox1.Refresh()
        End Sub
    
        Private Sub HScrollBar1_Scroll(sender As Object, e As ScrollEventArgs) Handles HScrollBar1.Scroll
            ptDest.X = ptOrigin.X + e.NewValue
            SpecialSegment.pt2 = ptDest
            PictureBox1.Refresh()
        End Sub
    
        Private Sub HScrollBar2_Scroll(sender As Object, e As ScrollEventArgs) Handles HScrollBar2.Scroll
            ptDest.Y = ptOrigin.Y + e.NewValue
            SpecialSegment.pt2 = ptDest
            PictureBox1.Refresh()
        End Sub
    End Class
    
    Class Segment
        Public pen1 As Pen
        Public pt1, pt2 As Point
    
        Public Sub New(pen As Pen, point1 As Point, point2 As Point)
            pen1 = pen
            pt1 = point1
            pt2 = point2
        End Sub
    
        Public Sub Draw(gr As Graphics)
            gr.DrawLine(pen1, pt1, pt2)
        End Sub
    End Class
    
    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. LesHay 7,141 Reputation points
    2022-01-16T09:21:11.84+00:00

    Hi

    Personally, I would use GraphicsPaths for doing this. I would create a new GraphicsPath for each element I want to draw, and and add each to a collection of GraphicsPath.
    The Paint event would loop the collection and draw each element. So, to erase an element it is as simple as removing it from the collection. Adding elements is just as straightforward by adding to the collection.
    Another benefit is that you can add/remove all sorts of shapes etc just as easily.
    If needed, the idea is easily expanded to have separate colors/pen widths etc for each element - this could be accomplished by creating a Class with the GraphicsPath and the various factors such as pen with, fill color, outline color etc - and adding new instances of the Class to a collection. In the Paint ebent, just draw the path using the various factors stored for each element in the Class instance.

    But, see Castorix suggestion for more advance cases.

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.