Solved

Finding a control

Posted on 2006-07-01
8
267 Views
Last Modified: 2008-02-01
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
Comment
Question by:broadbent
  • 3
  • 3
  • 2
8 Comments
 
LVL 10

Expert Comment

by:ericwong27
ID: 17022885
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
 
LVL 10

Expert Comment

by:ericwong27
ID: 17022897

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
 
LVL 4

Author Comment

by:broadbent
ID: 17023442
What if I don't know what the control is?
Windows knows because the MouseOver event happens.
Can I use this mechanism?
0
 
LVL 10

Expert Comment

by:ericwong27
ID: 17023659
   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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 125 total points
ID: 17023832
See the GetChildAtPoint() method:
http://msdn2.microsoft.com/en-us/library/a6zktd23.aspx
0
 
LVL 4

Author Comment

by:broadbent
ID: 17024085
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 17024162
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
 
LVL 4

Author Comment

by:broadbent
ID: 17026171
Oh well. Actually, I have found a better method, which involves adding a property to the embedded cntrols. Thanks anyway
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now