Solved

Find wich control is under the mouse

Posted on 2008-10-14
8
3,057 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 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
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 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
 

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

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!

Question has a verified solution.

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

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

749 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