我只是想知道在Visual Basic 2010 Express中是否可以循环,例如,一系列RectangleShapes,但是使用For ... Next循环并检查矩形的每个名称的特定属性。例如:
Private Sub RectangleIntersect(ByVal rect As Microsoft.VisualBasic.PowerPacks.RectangleShape)
For c = 1 To 31
RectangleShape('and then add the c value to the name).Location
Next
End Sub
非常感谢帮助。谢谢你的时间!
答案 0 :(得分:0)
通过您提供的代码块,我假设您在父类的某处有一堆名为RectangleClassN
的RectangleShape对象,其中N例如是1到31。
作为方法的参数,您需要的是某种RectangleShapes集合,而不是您定义为单个RectangleShape的集合。然后你就可以迭代它们了。
Private Sub RectangleIntersect(ByVal rectangles As IEnumerable(Of RectangleShape))
For Each rs As RectangleShape in rectangles
If rs.Location = "" Then
'something
End If
Next
End Sub
调用方法时,您需要传递包含多个对象的某种集合。请参阅下面的人为设计示例。
Dim myRectangles As New List(Of RectangleShape)()
myRectangles.Add(RectangleShape1)
myRectangles.Add(RectangleShape2)
RectangleIntersect(myRectangles)
由于收集每个单独的对象会很痛苦,你可以做一些事情,比如循环遍历属于某个容器的所有控件或对象并检查它们的类型。如果它们的类型是RectangleObject,请将它们添加到集合中。或者您甚至可以在RectangleIntersect方法中执行此代码。
For Each rs As RectangleShape In parentContainer.Controls ' or whatever this would be. Children? it depends on what the container is
...
Next
答案 1 :(得分:0)
假设您对所有RectangleShapes使用相同的ShapeContainer(默认为ShapeContainer1):
Private Sub RectangleIntersect(ByVal rect As Microsoft.VisualBasic.PowerPacks.RectangleShape)
For Each otherRect As PowerPacks.RectangleShape In ShapeContainer1.Shapes.OfType(Of PowerPacks.RectangleShape)()
If Not (otherRect Is rect) Then
If otherRect.Bounds.IntersectsWith(rect.Bounds) Then
Debug.Print(otherRect.Name & " intersects with " & rect.Name)
End If
End If
Next
End Sub
为了得到他们"按顺序"按照您的要求,您可以这样做:
Private Sub RectangleIntersect(ByVal rect As Microsoft.VisualBasic.PowerPacks.RectangleShape)
For c As Integer = 1 To 31
Dim c2 As Integer = c
Dim R As RectangleShape = ShapeContainer1.Shapes.OfType(Of PowerPacks.RectangleShape).FirstOrDefault(Function(x)
Return x.Name = "RectangleShape" & c2
End Function)
If Not IsNothing(R) AndAlso Not (R Is rect) Then
If R.Bounds.IntersectsWith(rect.Bounds) Then
Debug.Print(R.Name & " intersects with " & rect.Name)
End If
End If
Next
End Sub