[英]Get Child LineShape of Parent ShapeContainer
When I create a pair of LineShape and ShapeContainer objects, I make the ShapeContainer the parent using the code: 当创建一对LineShape和ShapeContainer对象时,我使用以下代码将ShapeContainer设置为父对象:
myLS.Parent = mySC
Me.Controls.Add(mySC)
Next, whenever the cursor position is near the end of the LineShape, I need to change the color of the line, and have started to implement the following code: 接下来,只要光标位置靠近LineShape的末端,我就需要更改线条的颜色,并开始实现以下代码:
Dim ClickedShapeContainerName As String = sender.Name
Dim siSCId As Integer
Dim myLS As New LineShape
Dim mySC As New ShapeContainer
'get ID of clicked ShapeContainer
For Each c As Control In Me.Controls
If c.Name = ClickedShapeContainerName Then
mySC = CObj(c)
If mySC.HasChildren Then
myLS =???????
siSCId = 1
End If
Exit For
End If
Next
If siSCId > -1 Then
If MouseIsNearBy(myLS.EndPoint) Then
myLS.BorderColor = Color.Red
NearLineEndPoint = True
End If
End If
Private Function MouseIsNearBy(ByVal testPoint As Point) As Boolean
testPoint = Me.PointToScreen(testPoint)
Return Math.Abs(testPoint.X - MousePosition.X) <= HitTestDelta AndAlso Math.Abs(testPoint.Y - MousePosition.Y) <= HitTestDelta
End Function
However, I cannot determine how to get the child LineShape of the parent ShapeContainer, so that I can set my new LineShape ("myLS") equal to the one found child. 但是,我无法确定如何获取父ShapeContainer的子LineShape,因此我可以将新的LineShape(“ myLS”)设置为等于找到的一个子项。 The mySC is successfully set to the clicked ShapeContainer, but I can't raise the child LineShape. mySC已成功设置为单击的ShapeContainer,但无法引发子LineShape。 How can this be done? 如何才能做到这一点?
You have to loop through the Shape collection, not Controls, of the ShapeContainer: 您必须遍历ShapeContainer的Shape集合,而不是Controls:
If Me.Controls.ContainsKey(clickedShapeContainerName) Then
mySC = DirectCast(Me.Controls(clickedShapeContainerName), ShapeContainer)
For Each ls As LineShape In mySC.Shapes
If MouseIsNearBy(ls.EndPoint) Then
ls.BorderColor = Color.Red
End If
Next
End If
You could also just subscribe to the LineShape's MouseEnter and MouseLeave events, too. 您也可以只订阅LineShape的MouseEnter和MouseLeave事件。
Also, ShapeContainer Class makes a special note: 另外, ShapeContainer类要特别注意:
Be careful that you do not create more than one ShapeContainer for each form or container; 注意不要为每个表单或容器创建多个ShapeContainer。 doing this may introduce unexpected behavior. 这样做可能会导致意外的行为。 If you add a design-time line or shape control to a form or container after you write code to create one programmatically, you should modify that code to use the ShapeContainer created by the designer. 如果在编写代码以编程方式创建窗体或容器后在窗体或容器中添加设计时线或形状控件,则应修改该代码以使用设计者创建的ShapeContainer。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.