Solved

Find wich control is under the mouse

Posted on 2008-10-14
8
2,990 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Library to convert HTML to PDF. 8 57
vb.net 2 37
VB.net/WinForms: adjust Screen Resolution 6 41
I need help making a collection from a class in VB.Net 3 26
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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

910 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

22 Experts available now in Live!

Get 1:1 Help Now