Solved

Find wich control is under the mouse

Posted on 2008-10-14
8
3,078 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
[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
  • 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 86

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

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
 

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 86

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 86

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

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…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

734 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