Link to home
Start Free TrialLog in
Avatar of broadbent
broadbentFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Finding a control

If I can give the x and y co-ordinates on a form, how can I code to return whatever control is visible at that point. I am looking for a control that might be embedded in another contrl and so on.
Avatar of ericwong27
ericwong27
Flag of Singapore image

Dim _Control As Control
 
 For Each _Control In Me.Controls

     If TypeOf _Control Is TextBox Then

          'Do Something
       
     End If

 Next

if control is embeded in panel

 For Each _Control In Me.Panel1.Controls

     If TypeOf _Control Is TextBox Then

         'Do Something
   
     End If

 Next



Here is the Sample Code maybe is suit to your question

   Public Sub SearchVisiblePanelLabel()

        For Each lControl As Control In Me.Panel1.Controls

            If TypeOf lControl Is Label Then
                If CType(lControl, Label).Visible Then
                    'Do something
                End If
            End If

        Next

    End Sub
Avatar of broadbent

ASKER

What if I don't know what the control is?
Windows knows because the MouseOver event happens.
Can I use this mechanism?
   When the control is invisible, I don't think you can access events

    If control is visible, just add your code in Mouse Hover event. When mouse is hover into the control your code will be fire
 
    Private Sub Label1_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles Label1.MouseHover
        Debug.WriteLine("Hover Label1")
    End Sub

    Private Sub Panel2_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles Panel2.MouseHover
        Debug.WriteLine("Hover Panel2")
    End Sub
 
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Just tried it inside my Tree UserControl

      Private Sub Tree_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
            Dim ctl As Control = Me.GetChildAtPoint(New Point(e.X, e.Y))
            If ctl Is Nothing Then Exit Sub
            ToolTip1.SetToolTip(Me, ctl.Text)
            Debug.WriteLine(ctl.Text)
      End Sub

ctl is always nothing
Correct...because MouseMove for your usercontrol will only fire when the mouse is NOT over one of your child controls inside the usercontrol...

The MouseMove event will be firing for whatever control the mouse is currently over.   If you had Label1, then you would get:

    Private Sub Label1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseMove

     End Sub

...whenever the mouse moved over it.  So when you "leave" the usercontrol and move over an "embedded" control, the MouseMove sub that fires will change from the usercontrol to that control.

If you want to be able to trap mouse movement for ALL the controls inside your usercontrol, then perhaps you could iterate over the controls and use the AddHandler() function to wire up all the MouseMove events to the same routine.

I'm not at my computer right now (I'm moving and am staying at a friends house) so I can't give you exact code.

There is an equivalent for the Form_Load() event for a UserControl:

     ' (in the UserControl "Load" event...don't remember what it's called)
     For each ctl As Control In Me.Controls
        AddHandler ctl.MouseMove, AddressOf Me.Control_MouseMove
     Next ctl

So now, when the mouse moves in any control, the sub below should fire:

     Private Sub Control_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
         Dim ctl As Control = CType(sender, Control)

         ' Use "ctl" somehow...it will tell you which control the mouse is moving in...

     End Sub
Oh well. Actually, I have found a better method, which involves adding a property to the embedded cntrols. Thanks anyway