Search code examples
vb.netdrawrectangle

what do I need to pass to this Subroutine in vb.net?


Public Sub DrawRectangleInt(e As PaintEventArgs)

        ' Create pen. 
        Dim blackPen As New Pen(Color.Black, 3)

        ' Create location and size of rectangle. 
        Dim x As Integer = 0
        Dim y As Integer = 0
        Dim width As Integer = 200
        Dim height As Integer = 200

        ' Draw rectangle to screen.
        e.Graphics.DrawRectangle(blackPen, x, y, width, height)

    End Sub

when calling the Sub with:

DrawRectangleInt()

I get an error saying that I need to pass something for 'e', but what?

Thanks.


Solution

  • You eaither call that sub from a paint event and pass the e variable to your sub, or create the Graphics object inside your sub. The Using/End Using blocks dispose of the objects correctly.

    Public Sub DrawRectangleInt()
    
      ' Create pen. 
      Using blackPen As New Pen(Color.Black, 3)
    
        ' Create location and size of rectangle. 
        Dim x As Integer = 0
        Dim y As Integer = 0
        Dim width As Integer = 200
        Dim height As Integer = 200
    
        ' Draw rectangle to screen.
        Using g As Graphics = Me.CreateGraphics
          g.DrawRectangle(blackPen, x, y, width, height)
        End Using
      End Using
    End Sub