Solved

Finding a control

Posted on 2006-07-01
8
275 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

631 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