Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 280
  • Last Modified:

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.
0
broadbent
Asked:
broadbent
  • 3
  • 3
  • 2
1 Solution
 
ericwong27Commented:
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


0
 
ericwong27Commented:

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
0
 
broadbentAuthor Commented:
What if I don't know what the control is?
Windows knows because the MouseOver event happens.
Can I use this mechanism?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
ericwong27Commented:
   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
 
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
See the GetChildAtPoint() method:
http://msdn2.microsoft.com/en-us/library/a6zktd23.aspx
0
 
broadbentAuthor Commented:
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
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
0
 
broadbentAuthor Commented:
Oh well. Actually, I have found a better method, which involves adding a property to the embedded cntrols. Thanks anyway
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now