ContextMenuStrip Source

I've done extensive research online, heavily studied the ContextMenuStrip and related controls, but I can not figure out which control was right clicked on to get the ContextMenuStrip to display  I basically want ContextMenu.SourceControl but for ContextMenuStrip. If anyone can help it would be MUCH appreciated.

Thanks


Answer this question

ContextMenuStrip Source

  • wkimes369852147

    I know this may seem silly, but how would you write that in VB

    -Paul.

  • Agalab

    It's ok I have discovered how to do it thanks.

    -Paul

    Private Sub ContextMenuStrip1_Opening(ByVal sender As System.Object, ByVal e As _ System.ComponentModel.CancelEventArgs) Handles ContextMenuStrip1.Opening

            Dim cntrlSource As Control
            cntrlSource = ContextMenuStrip1.SourceControl

    End Sub



  • Ranj

    Hi :)

    Here's what you need to do, you need to use the Opening event of the ContextMenuStrip, like so:

    private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
    {
         srcControl = ((ContextMenuStrip)sender).SourceControl;
    }

    This will work.

    I guess the reasoning behind it is that when you first create an instance of contextMenuStrip, it doesn't belong to anyone and only gets shown when a control requests it. Hence, only then will it know who invoked it and not before.

    If guys from MS are listening, this example is a perfect candidate for the MSDN docs, eh

    --p

  • Tobias Zimmergren


    Good post - the original problem was we didn't have an Opening or a SourceControl property. We added ContextMenuStrip.SourceControl property after B1. Thanks - I've forwarded this content to our doc people as well. 

    - Erick

  • ContextMenuStrip Source