?
Solved

Find wich control is under the mouse

Posted on 2008-10-14
8
Medium Priority
?
3,150 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
Industry Leaders: 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 2000 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
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…
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month10 days, 14 hours left to enroll

770 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