Question Polygon Drawing Errors

dijondude

New member
Joined
Jul 5, 2012
Messages
3
Programming Experience
Beginner
So, I for the life of me, cannot draw a polygon in VB. I am semi-new to programming, but I have a understanding of lot of the concepts. Here's my code so far (a bit shrunken though, to the parts that affect the drawing):

Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles MyBase.Load


Me.polygonTest()

End Sub

Private Sub polygonTest(ByVal e As System.Windows.Forms.PaintEventArgs)


Dim point1 As New Point(2, 3)
Dim point2 As New Point(100, 300)
Dim point3 As New Point(70, 55)
Dim pts() As Point = {New Point(point1), New Point(point2), New Point(point3)}
e.Graphics.DrawPolygon(Pens.Black, pts)
End Sub

When running this, it says I need to pass something to e. The only thing I've found to actually pass to it for some reason is a Dim'd variable that doesn't have a data type.

Any tips or solutions?
 
Your method requires an argument "e" of type system.windows.forms.painteventargs

It is no different to

VB.NET:
Private Sub Test(byval s as string)
End Sub

which requires as string.

If I was to then call Test(), I would get an error as I did not pass in a string argument.
 
Your method requires an argument "e" of type system.windows.forms.painteventargs

It is no different to

VB.NET:
Private Sub Test(byval s as string)
End Sub

which requires as string.

If I was to then call Test(), I would get an error as I did not pass in a string argument.

Thanks, but what data type should I pass to it then? I've tried using a couple (like Object and Rectangle), but I always get an error saying it's the wrong type.
 
I had to double check this, been a long time since I did drawing!

You need to call your code on the paint event of an object, the example below is on the form paint event.

VB.NET:
    Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
        Dim point1 As New Point(2, 3)
        Dim point2 As New Point(100, 300)
        Dim point3 As New Point(70, 55)
        Dim pts As Point() = {point1, point2, point3}
        Dim p As New Pen(Brushes.Black)
        e.Graphics.DrawPolygon(p, pts)
    End Sub

Now if you really want to call it from your own method, on the paint event of the object (form, control, whatever) you could call pass in that events arguments to your method like below

VB.NET:
    Private Sub PolygonTest(e As PaintEventArgs)
        Dim point1 As New Point(2, 3)
        Dim point2 As New Point(100, 300)
        Dim point3 As New Point(70, 55)
        Dim pts As Point() = {point1, point2, point3}
        Dim p As New Pen(Brushes.Black)
        e.Graphics.DrawPolygon(p, pts)
    End Sub
    Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
        PolygonTest(e)
    End Sub
 
I had to double check this, been a long time since I did drawing!

You need to call your code on the paint event of an object, the example below is on the form paint event.

VB.NET:
    Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
        Dim point1 As New Point(2, 3)
        Dim point2 As New Point(100, 300)
        Dim point3 As New Point(70, 55)
        Dim pts As Point() = {point1, point2, point3}
        Dim p As New Pen(Brushes.Black)
        e.Graphics.DrawPolygon(p, pts)
    End Sub

Now if you really want to call it from your own method, on the paint event of the object (form, control, whatever) you could call pass in that events arguments to your method like below

VB.NET:
    Private Sub PolygonTest(e As PaintEventArgs)
        Dim point1 As New Point(2, 3)
        Dim point2 As New Point(100, 300)
        Dim point3 As New Point(70, 55)
        Dim pts As Point() = {point1, point2, point3}
        Dim p As New Pen(Brushes.Black)
        e.Graphics.DrawPolygon(p, pts)
    End Sub
    Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
        PolygonTest(e)
    End Sub


Thanks! Got it all working :)
 
Back
Top