我想循环遍历窗体控件RectangleShape,但是当我编写控件的类型((RectangleShape))时,它们会显示一个ereur
For Each cnt As Control In Me.Controls
If TypeOf cnt Is RectangleShape Then
End If
Next
他们告诉我这个ereeur:“类型'RectangleShape'没有定义。”
它也适用于我使用控件类型TextBox或VS FORM中的任何其他控件,如下所示:
For Each cnt As Control In Me.Controls
If TypeOf cnt Is TextBox Then
End If
Next
答案 0 :(得分:2)
当然只需将cnt设为对象,以便编译器可以强制转换:
For Each cnt As Object In Me.Controls
If TypeOf cnt Is RectangleShape Then
End If
Next
这是因为System.Windows.Control
和Microsoft.VisualBasic.PowerPacks.RectangleShape
答案 1 :(得分:1)
拼出完整的姓名:
If TypeOf cnt Is Microsoft.VisualBasic.PowerPacks.RectangleShape Then
或者将它放在源代码文件的顶部
Imports Microsoft.VisualBasic.PowerPacks
但由于RectangleShape不是控件,因此无效。它由名为ShapeContainer的类托管。你需要这样的代码:
For Each ctl In Me.Controls
If TypeOf ctl Is ShapeContainer Then
For Each shape In DirectCast(ctl, ShapeContainer).Shapes
If TypeOf shape Is RectangleShape Then
Dim rect = DirectCast(shape, RectangleShape)
'' do something
''...
End If
Next
End If
Next
您可能希望利用设计人员为矩形创建命名变量的能力。
答案 2 :(得分:0)
形状确实是ShapeContainer对象的成员。可以通过查看表单上的对象来检测到这一点,但是存在一个问题:形状可以放置在任何容器对象(如面板和组框)中,因此,如果形状中包含形状,则需要大量嵌套到最深层次当将第一个形状放入该面板或组框时,Shapecontainer便成为此类容器内的对象。因此,编号与面板或组框的编号无关。 我想有更好的方法来获取嵌套(使用其他语言?),我尝试过使用数组,但这没有用。
For Each ctl In Me.Controls
If TypeOf ctl Is ShapeContainer Then
For Each shape In DirectCast(ctl, ShapeContainer).Shapes
'do something
Next
Elseif TypeOf ctl Is Panel or Typeof ctl is GroepBox Then
For Each ctl2 In DirectCast(ctl, Control).Controls
If TypeOf ctl2 Is ShapeContainer Then
For Each shape In DirectCast(ctl2, ShapeContainer).Shapes
'do something
Next
Elseif TypeOf ctl2 Is Panel or Typeof ctl2 is GroepBox Then
For Each ctl3 In DirectCast(ctl2, Control).Controls
If TypeOf ctl3 Is ShapeContainer Then
For Each shape In DirectCast(ctl3, ShapeContainer).Shapes
'do something
Next
Elseif TypeOf ctl3 Is Panel or Typeof ctl3 is GroepBox Then
'goto next level...
End if
Next
End If
Next
End If
Next