Polygon

Haegendoorn

New member
Joined
Apr 7, 2008
Messages
4
Programming Experience
Beginner
Hi,

I have to develop an application that creates a polygon. The points of the polygon should be defined by several random mouse clicks inside the picturebox.

My problem is that i can t dynamically add points to an array of points because i get the error: 'Object reference not set to an instance of an object.'
This is my code

VB.NET:
Dim MyPoints() As Point
    Dim Clicks As Integer = 0
   
    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
        ' add point to polygon
        MyPoints(Clicks) = New Point(Windows.Forms.Cursor.Position.X - Me.Location.X, _
            Windows.Forms.Cursor.Position.Y - Me.Location.Y - 27)
        Clicks += 1
        If Clicks > 2 Then
            DrawPolygon()
        End If
    End Sub

    Sub DrawPolygon()
        Dim BM As New Bitmap(250, 250)
        Dim G As Graphics = Graphics.FromImage(BM)
        G.DrawPolygon(Pens.Black, MyPoints)

        PictureBox1.Image = BM
    End Sub
 
Use an arraylist or a list instead of an array, then you could do MyPoints.Add(new Point(x,y)).

Thanks for your quick response.

But when i want to call the g.DrawPolygon method i need a 1-dimensional array of points. How should I solve this new problem?

With kind regards
 
Arraylist has a .ToArray() method that you can use in the g.DrawPolygon parameter. Instead of passing MyPoints, pass MyPoints.ToArray(). I believe lists have the same function.
 
Back
Top