# Draw a line in visual basic.net and overrides



## Percey (Jul 3, 2007)

Hello. Im having trouble drawing a line in a picturebox in visual basic.net. at first I just wanted to see if i could, but now its annoying...anyway. So ive been looking around the web and I see some of the same answers and relatively the same ideas on the subject, but no concrete "this is how" answers...I've been toying with it for a while and still can't do it. so...this is what I want to do

Ive written a procedure that will get the start and end coordinates (it's a little sketchy but im not too worried about it right now)

```
'Form level
Private formGraphics As System.Drawing.Graphics
Private myPen As New System.Drawing.Pen(System.Drawing.Color.Red)

.
.
.

    Private Sub setgetCoords(ByVal getOrSet As Boolean)
        If getOrSet Then
            intStartX = System.Windows.Forms.Cursor.Position.X
            intStartY = System.Windows.Forms.Cursor.Position.Y
        Else
            intEndX = System.Windows.Forms.Cursor.Position.X
            intEndY = System.Windows.Forms.Cursor.Position.Y

        End If

    End Sub
```
so then once ive done that, im stuck as to where to put this


```
formGraphics.DrawLine(myPen, intStartX, intStartY, intEndX, intEndY)
```
Also, I've heard quite a lot of mention (on MSDN and other sources) of overriding the onPaint method...



> Override the Form's OnPaint method. Use e.graphics to draw graphics.
> 
> Outside the OnPaint procedure:
> dim g as graphics
> ...


but that means nothing to me as i have no idea how to use that or what it even is...can anyone offer some help with this matter?


----------



## artur02 (Sep 24, 2007)

Overriding end events are the basic concepts of object-oriented (OO) development and .NET programming.

If you override the onPaint method (event), your code will be executed instead of the built-in code.
Events are generated when you click a button, move your mouse, press a button, etc.

I think it would be much more easier for you to create an event handler for the Paint event, so you don't have to use OO things.


Go to code view
Select your PictureBox from the left combo box
Select the Paint event from the right combo box
Something like this will be created:

```
Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
        ...
End Sub
```

Add the following line before the End Sub line:

```
e.Graphics.DrawLine(Pens.Black, 5, 5, 50, 50)
```


----------



## Percey (Jul 3, 2007)

Yeah I know how OOP works, i just didn't know what overrides were...

I used


```
Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
        e.Graphics.DrawLine(Pens.Black, 5, 5, 50, 50)
    End Sub
```
and it worked...however i want to make it so that I am able to click and drag to draw the line..so when is "Picturebox1.Paint" executed?


----------



## artur02 (Sep 24, 2007)

What is your programming experience? How deep should I go into details?

OnPaint is called whenever the window should be repainted. So you should always do custom drawings in the Paint event.

If you want to handle clicks, there is a Click event. You can subscribe to is the same way you did with the Paint event. Maybe you should rather use MouseDown and MouseUp events.

All the rest is about algorithms.

You can check event descriptions in Visual studio Help and on MSDN, like OnPaint method at http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.onpaint.aspx and like Paint event at http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.paint.aspx.


----------



## Percey (Jul 3, 2007)

Yeah I know how OOP works, i just didn't know what overrides were...

I used


```
Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
        e.Graphics.DrawLine(Pens.Black, 5, 5, 50, 50)
    End Sub
```
and it worked...however i want to make it so that I am able to click and drag to draw the line..so when is "Picturebox1.Paint" executed?


----------



## 2-MAS (Dec 27, 2007)

You will need four integer variables to store coodinates. Two integers for where you do the click event (x1, y1) (get these values during the event for mouse button pressed) and two more variable to store the coordinates for where you release the mouse button (x2, y2) (get these values during the event for mouse button released)

TADA 

now use those four integer values here : e.Graphics.DrawLine(Pens.Black, 5, 5, 50, 50)

If you want to draw more than lines, you might want to make a class for storing all the different coordinates and diameters and stuffs


----------

