Solved

Find wich control is under the mouse

Posted on 2008-10-14
8
2,974 Views
Last Modified: 2012-05-05
Hi experts,

I'm looking for a way to find which control is under the mouse when I right click in the form.
I want to customize a context menu depending of which control is under the cursor at the right click.
I use this code:
Dim p As Point = PointToClient(New Point(ContextMenuStrip1.Left, ContextMenuStrip1.Top))
Dim c As System.Windows.Forms.Control = GetChildAtPoint(p, GetChildAtPointSkip.None)

This works except when I'm in a tab (I use Infragistics tab control), if the control is in a tab, I always get the tabcontrol as the control found.
Is this particular to Infragistics and is there a workaround or another trick to find the control?

Thanks
0
Comment
Question by:hqdev
  • 4
  • 4
8 Comments
 

Author Comment

by:hqdev
ID: 22715019
I've tried it  with standard TabControl and I get the same result.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 22715771
In this line:

    Dim c As System.Windows.Forms.Control = GetChildAtPoint(p, GetChildAtPointSkip.None)

There is an Implied "Me" in front of GetChildAtPoint().  So what you are really asking is what child is under the mouse but on the FORM.  Since each container has its own Controls() collection you have to call this recursively.  It will only return controls that are directly contained by the Form.  If the control returned has children then you need to call the GetChildAtPoint() function with it to see if there is a control at that location:

    x = returnedControl.GetChildAtPoint(...)

See my example here for more details:
http://www.experts-exchange.com/Developer/Programming/Languages/.NET/Visual_Basic.NET/Q_23797396.html#22670308
0
 

Author Comment

by:hqdev
ID: 22719890
Hi, thanks for your reply but,
I had already tried that, and at the second pass the tabcontrol has childrens but the GetChildAtPoint return nothing.

I tried your function anyway and got the same result, here's the code:

Private Sub ContextMenuStrip1_Opening(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles ContextMenuStrip1.Opening
  Dim p As Point = PointToClient(New Point(ContextMenuStrip1.Left, ContextMenuStrip1.Top))
  Dim c As Control = FindControl(Me, p)
End Sub

Private Function FindControl(ByVal cont As Control, ByVal p As Point) As Control
  Dim ctl As Control = cont.GetChildAtPoint(p, GetChildAtPointSkip.None)
  If Not IsNothing(ctl) Then
    If ctl.HasChildren Then
      Dim subCtl As Control = FindControl(ctl, p)
      If Not IsNothing(subCtl) Then
        Return subCtl
      Else
        Return ctl
      End If
    Else
      Return ctl
    End If
  Else
    Return Nothing
  End If
End Function
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 22721149
Are you just trying to figure out which control the user Right Clicked on to make the ContextMenuStrip to appear?

    Private Sub ContextMenuStrip1_Opening(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles ContextMenuStrip1.Opening
        Dim ctl As Control = ContextMenuStrip1.SourceControl
        Debug.Print("Source: " & ctl.Name)
    End Sub
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:hqdev
ID: 22721300
That's exactly what I'm trying to do, but the sourcecontrol property doesn't always return the right control, 90% of the time I get the main form instead of the control right-clicked.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 22721694
You said:

    "I use Infragistics tab control"

99% of the time when I see that someone is experiencing an "odd" problem and a third-party control is involved...the problem magically goes when either:

(a) They get rid of the external control.
(b) They contact the manufacturer and find out there is a fix (or it is a known "bug").

With this comment:

    "90% of the time I get the main form instead of the control right-clicked."

Are 90% of your controls located in the Infragistics tab control?

0
 

Author Closing Comment

by:hqdev
ID: 31506330
How silly of me!
I have hooked my contextmenu to the main form and a forgot to hook the contextmenu to 90% of my controls, that's the 90% failure!!

So the sourceControl property is working perfectly!

I'm gona use a different contextmenu for the page with the same items and this one for my controls and this will do the trick!

Thanks you very much "Idle Mind", it was Soooo simple, I'm a little bit ashame of myself :( a little too tired I think.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 22722146
Lol...glad you found the problem...and we've ALL done similar things...MYSELF included.  =)
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

758 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

21 Experts available now in Live!

Get 1:1 Help Now