如何检查GroupBox中是否存在对象 - VB.NET

时间:2012-01-05 21:46:17

标签: vb.net object

如何检查GroupBox中是否存在一个对象(在这种情况下我的对象是一个ButtonBox),但是在一个确切的位置。

这样的事情:

If Groupbox1.NameOfButtonBox.location(40,190) exists then 
      Do my code 
end if 

我知道语法是完全错误的,但它只是一个例子

2 个答案:

答案 0 :(得分:1)

为了将其用于按钮类型而不是按钮的特定实例,您需要检查找到的控件的类型。您还需要确保那里有控件。这是一个函数,它将检查组合框是否包含给定坐标的按钮。

Private Function ButtonExists(ByVal group As GroupBox, ByVal x As Integer, ByVal y As Integer) As Boolean

    'No sense checking if there isn't a group box
    If group Is Nothing Then
        Return False
    End If

    'Find the control at the given point
    Dim ctrl As Control = group.GetChildAtPoint(New Point(x, y))

    'If there is a control at that point check to see if it's a button
    If ctrl IsNot Nothing AndAlso TypeOf (ctrl) Is Button Then
        Return True
    End If

    Return False
End Function

答案 1 :(得分:0)

查看GetChildAtPoint:

Dim ctrl As Control = GroupBox1.GetChildAtPoint(New Point(147, 96))
If TypeOf ctrl Is Button Then
    Dim btn As Button = DirectCast(ctrl, Button)
    'Do your code
End If

MSDN: Control.GetChildAtPoint Method