而不是在窗體設計器手動添加的線條,考慮編程將它們添加
Const N As Integer = 50
Dim _lines(N - 1) As LineShape
Dim _numbers(N - 1) As Integer
Private Sub frmLineShapes_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: Fill the _numbers array with values.
For i As Integer = 0 To N - 1
_lines(i) = New LineShape With { _
.X1 = 5 * i + 10, .Y1 = 20, _
.X2 = 5 * i + 10, .Y2 = 60, _
.BorderColor = DirectCast(IIf(_numbers(i) = 3, Color.Blue, Color.Black), Color) _
}
Next
Me.SuspendLayout()
Me.ShapeContainer1.Shapes.AddRange(_lines)
Me.ResumeLayout()
End Sub
現在,您已經擁有數組中的行,並且可以輕鬆訪問並更改它們。
注:
在Visual Basic的PowerPack的形狀並不直接添加到形式;相反,它們被添加到形狀容器。您必須在窗體設計器中爲窗體添加至少一個形狀,以便VB自動將形狀容器添加到窗體。如果沒有,你仍然可以通過編程來添加它。
Me.SuspendLayout()
Dim ShapeContainer1 = New ShapeContainer
Me.Controls.Add(ShapeContainer1)
ShapeContainer1.Shapes.AddRange(_lines)
Me.ResumeLayout()
UPDATE:
如果手動添加這些行,你仍然可以通過他們的名字
For i As Integer = 0 To N - 1
Dim index As Integer = Me.ShapeContainer1.Shapes.IndexOfKey("LineShape" & (i + 1))
Dim line As LineShape = DirectCast(Me.ShapeContainer1.Shapes(index), LineShape)
If _numbers(i) = 3 Then
line.BorderColor = Color.Blue
Else
line.BorderColor = Color.Black
End If
Next
我添加了一個例子,它顯示瞭如何將線接入線可以通過他們的名字進行訪問。 – 2012-02-17 14:10:06