自定义属性不是控件[vb.net]的成员
我是vb.net的新手,不知道我是否做得对 在我的程序中,我创建了一个自定义控件(自定义属性不是控件[vb.net]的成员,vb.net,visual-studio-2015,Vb.net,Visual Studio 2015,我是vb.net的新手,不知道我是否做得对 在我的程序中,我创建了一个自定义控件(CustomControl),它有一个自定义属性(CustomProperty) 在程序中,我有一个For-Each语句,用于检查表单中的每个CustomControl,并在满足某些条件时更改CustomProperty的值: For Each _CustomControl as Control in Controls If TypeOf _CustomControl is CustomControl an
CustomControl
),它有一个自定义属性(CustomProperty
)
在程序中,我有一个For-Each语句,用于检查表单中的每个CustomControl
,并在满足某些条件时更改CustomProperty
的值:
For Each _CustomControl as Control in Controls
If TypeOf _CustomControl is CustomControl and [criteria met]
_CustomControl.CustomProperty = value
End If
next
每当我输入第三行时,它会给我以下消息:
“CustomProperty”不是“Control”的成员
我知道我的自定义属性通常不属于“控件”,我想知道是否应该向代码中添加一些内容,或者是否应该以其他方式键入它
非常感谢您提供的任何帮助。您必须根据自己的情况使用:
For Each _CustomControl As Control In Controls
If TypeOf _CustomControl Is CustomControl AndAlso [criteria met]
_CustomControl.CustomProperty = value
End If
Next
您尝试访问默认控件上的CustomProperty
。使用和时,如果第一部分不正确,则不计算条件的第二部分
您必须根据自己的情况使用:
For Each _CustomControl As Control In Controls
If TypeOf _CustomControl Is CustomControl AndAlso [criteria met]
_CustomControl.CustomProperty = value
End If
Next
您尝试访问默认控件上的CustomProperty
。使用和时,如果第一部分不正确,则不计算条件的第二部分
给出的答案很好,但更好的方法是使用类型为
的预先过滤掉不需要的控件。这使得类型检查变得不必要
For Each _CustomControl in Controls.OfType(Of CustomControl)
If [criteria met]
_CustomControl.CustomProperty = value
End If
Next
如果您不想使用该选项,则在尝试访问CustomProperty
之前,需要强制转换为CustomControl
类型,如下所示:
For Each _CustomControl As Control In Controls
If TypeOf _CustomControl Is CustomControl And [criteria met]
DirectCast(_CustomControl,CustomControl).CustomProperty = value
End If
Next
给出的答案很好,但更好的方法是预先使用of type
过滤掉不需要的控件。这使得类型检查变得不必要
For Each _CustomControl in Controls.OfType(Of CustomControl)
If [criteria met]
_CustomControl.CustomProperty = value
End If
Next
如果您不想使用该选项,则在尝试访问CustomProperty
之前,需要强制转换为CustomControl
类型,如下所示:
For Each _CustomControl As Control In Controls
If TypeOf _CustomControl Is CustomControl And [criteria met]
DirectCast(_CustomControl,CustomControl).CustomProperty = value
End If
Next