Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Finding a control

Posted on 2006-07-01
8
Medium Priority
?
278 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Accepted Solution

by:
Mike Tomlinson earned 500 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 86

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

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

688 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