将字符串列表转换为点数组 [英] Convert list of string to point array
本文介绍了将字符串列表转换为点数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨
我有一个X:Y坐标列表被捕获到一个新列表(字符串),
数量哪个是未知的。
我想将其转换为类型的Point数组。
这就是我所拥有的:
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 :
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
我的问题是CoOrds的结果是1010,0而不是10,10。
任何人都可以建议我如何解决这个问题。非常有必要。
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.
推荐答案
您不需要列表< string>
,而是列表与LT;点和GT;
。这将避免使用数组及其redim方法(这是昂贵的)。
建议修改:
You don't need aList<string>
, but rather aList<Point>
. This would moreover avoid the use of an array and its redim method (which is costly).
Suggested modification:
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.
这篇关于将字符串列表转换为点数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文