Click here to Skip to main content
15,893,904 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I have a list of X:Y co-ordinates that are captured into a New List(of String),
the quantity of which are unknown.
I would like to then convert this into a type Point array.

This is what I have :

VB
    Dim CoOrds() As Point
    Dim PointList As New List(Of String)


'PointList is populated like this (after the click of a button

 PointList.Add(txt_Add_Point_X.Text & "," & txt_Add_Point_Y.Text)

'Then at a given point a button is clicked to draw a polygon using gathered co-ordinates

    Private Sub but_Draw_Click(sender As System.Object, e As System.EventArgs) Handles but_Draw.Click
        Dim myGraphics As Graphics = Panel1.CreateGraphics

        ReDim CoOrds(PointList.Count)

        For x = 0 To PointList.Count - 1
            CoOrds(x) = (New Point(PointList(x)))   '<----Problem here
        Next

        myGraphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
        myGraphics.DrawPolygon(Pens.DarkRed, CoOrds)

    End Sub


My problem is that CoOrds ends up with a value like 1010,0 instead of 10,10.

Could anyone please suggest how I can fix this. Much obliged.
Posted

1 solution

You don't need a List<string>, but rather a List<Point>. This would moreover avoid the use of an array and its redim method (which is costly).

Suggested modification:
VB
Dim CoOrds As New List(Of Point)

'CoOrds is populated like this (after the click of a button)
Dim x, y As Integer
If (Int32.TryParse(txt_Add_Point_X.Text, x) AndAlso Int32.TryParse(txt_Add_Point_Y.Text, y))
   CoOrds.Add(new Point(x, y))
End If

'Then at a given point a button is clicked to draw a polygon using gathered co-ordinates
Private Sub but_Draw_Click(sender As System.Object, e As System.EventArgs) Handles but_Draw.Click
   Dim myGraphics As Graphics = Panel1.CreateGraphics

   myGraphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
   myGraphics.DrawPolygon(Pens.DarkRed, CoOrds.ToArray())
End Sub

Hope this helps.
 
Share this answer
 
v2
Comments
Darrell de Wet 27-Oct-15 5:29am    
You're a star. Thanks. Works well.
phil.o 27-Oct-15 5:32am    
You're welcome.
Maciej Los 27-Oct-15 6:05am    
5ed!
phil.o 27-Oct-15 6:07am    
Thanks :)
Darrell de Wet 27-Oct-15 9:57am    
Phil.o responded to my request within about 12 minutes, he provide the right answer the first time, was perfectly polite, and certainly never made me feel like a fool. Deserved all the accolades that he got, I think.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900