在vb.net中以图形方式检测表单上的控件

时间:2019-04-16 15:18:38

标签: vb.net user-interface controls

所以我有这种形式,用户可以在其中拖放用户控件(仅是图片框)。他们不能将控件放在另一个控件上,因为它们必须按顺序放置图片。这是我当前的代码: “ TempControl”是在“ for each”循环中检查的对象 “控件”是用户刚刚删除的对象

        ' vertical check: there's two lines possible
            If Control.Top < 200 Then
                Control.Top = 70
            Else
                Control.Top = 240
            End If
        ' horizontal check:
        For Each tempControl As MyUSerControl In MyArray
                If tempControl.Left < Control.Left And tempControl.Left + tempControl.Width > Control.Left Then    'If the left limit of the moved object is in the fix object                   
                        Control.Left = tempControl.Left + tempControl.Width + 5    'Put it on the fix object's right side
                End If

                If tempControl.Left > Control.Left And tempControl.Left < Control.Left + Control.Width Then  'If the right limit of the moved object is in the fix object        
                        Control.Left = tempControl.Left - Control.Width - 5    'Put it on the left side
                End If
        Next

(目前)它工作正常,但是我想知道是否有任何方法可以“图形化”地验证位置,例如检查“控件”下的表单是否免费的函数?

如果不是,是否有针对此类问题的更优化算法?您对此有何看法? 谢谢!

1 个答案:

答案 0 :(得分:0)

我不确定我是否正确理解了您的问题,但是让我们看看这是否有帮助。

GetChildAtPoint(Point)

  

在指定位置检索子控件。

示例:

Dim controlAtPoint = myForm.GetChildAtPoint(anyPoint)