I'm writting program in VB 2010, but I have a problem. I want to draw a temporary line on PictureBox control MouseMove event. That works fine but deleting of that line doesn't work. I tried to draw the white line on the same coordinates (PictureBox's background is white), but won't work.

Here is the code:

Private Sub drawing_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles drawing.MouseMove
        coordinates.Text = "X = " & e.X & "; Y = " & e.Y & ";"

        If tool = 0 Then
            Dim graphic As Graphics = drawing.CreateGraphics
            Dim pen As New Pen(Brushes.Red, 3)
            Dim eraser As New Pen(Brushes.White, 3)


            If A.X = 0 And A.Y = 0 Then
                Exit Sub
            Else
                B.X = e.X
                B.Y = e.Y

                With A1
                    .X = e.X
                    .Y = e.Y
                End With

                With B1
                    .X = e.X
                    .Y = e.Y
                End With

                If A.X = A1.X And B.X = B1.X Then
                    graphic.DrawLine(pen, A, B)
                Else
                    graphic.DrawLine(eraser, A1, B1)
                    graphic.DrawLine(pen, A, B)
                End If
            End If
        End If
    End Sub

Recommended Answers

All 7 Replies

can you post a snapshot of the form and the picture-box?
anyways if i got you right then maybe this will help you out
preserve the last B and put it in B1

With B1
                    .X = B.X
                    .Y = B.Y
                End With

               With B
                    .X = e.X
                    .Y = e.Y
               End With

then draw the white line

graphic.DrawLine(eraser, [B]A[/B], B1)

i tried it and it worked well.

I tried but that won't work, here's the screenshot of the form:
Picture

My problem is when I set A coordinates, and move mouse it draws me a line but it won't delete it. I need to fix this urgently, because I can't continue to work on other parts of the application.

Anyone???

it worked for me !
make sure you save the last B in B1 before you give B a new value !
that's the trick about it
the line must begin in A and end in the LAST B
so you have to save B in B1 and then draw a line from A to B1
so it's

graphic.DrawLine(eraser, A, B1)

and not

graphic.DrawLine(eraser, A1, B1)

can you try this function :
it save B in B1 then "erases" the last drawn line

Private Sub drawing_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles drawing.MouseMove
        coordinates.Text = "X = " & e.X & "; Y = " & e.Y & ";"

        If tool = 0 Then
            Dim graphic As Graphics = drawing.CreateGraphics
            Dim pen As New Pen(Brushes.Red, 3)
            Dim eraser As New Pen(Brushes.White, 3)


            If A.X = 0 And A.Y = 0 Then
                Exit Sub
            Else
                B1.X = B.X
                B1.Y = B.Y

                With B
                    .X = e.X
                    .Y = e.Y
                End With


                graphic.DrawLine(eraser, A, B1)
                graphic.DrawLine(pen, A, B)

            End If
        End If
    End Sub

Thanks, now it works. Problem in mine code was that is saved new B coordinates instead of old. Instead Exit Sub you need to put:

A.X = e.X
B.X = e.X

Last time I saved all the coordinates but it won't work. Now works!!! Again, thanks!

yeah that's a nice code snippet after all : )
good luck with the rest of the code : D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.