Which control is producing the ContextMenuStrip

  • Thread starter Thread starter Dom
  • Start date Start date
D

Dom

I have severall treeview controls on my form, and each has the
contextmenustrip property set to mnuMain. That is, if you right-click
on any tree, you get the same contextmenu. Is there some way, in
mnuMain_Opening, that I can tell WHICH tree is opening the menu?

Dom
 
You need to use the ".SourceControl" property in the "Opening" event of the
MenuStrip.
In the MSDN doc I found the following example that seems to work in my
VS2005 version:

Jean Lussier
Montreal (Canada)

--------------------------------------------------------------------------------------
Sub cms_Opening(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs)

' Acquire references to the owning control and item.
Dim c As Control = fruitContextMenuStrip.SourceControl
Dim tsi As ToolStripDropDownItem = fruitContextMenuStrip.OwnerItem

' Clear the ContextMenuStrip control's
' Items collection.
fruitContextMenuStrip.Items.Clear()

' Check the source control first.
If Not (c Is Nothing) Then
' Add custom item (Form)
fruitContextMenuStrip.Items.Add(("Source: " +
c.GetType().ToString()))
ElseIf Not (tsi Is Nothing) Then
' Add custom item (ToolStripDropDownButton or ToolStripMenuItem)
fruitContextMenuStrip.Items.Add(("Source: " +
tsi.GetType().ToString()))
End If

' Populate the ContextMenuStrip control with its default items.
fruitContextMenuStrip.Items.Add("-")
fruitContextMenuStrip.Items.Add("Apples")
fruitContextMenuStrip.Items.Add("Oranges")
fruitContextMenuStrip.Items.Add("Pears")

' Set Cancel to false.
' It is optimized to true based on empty entry.
e.Cancel = False
End Sub
 
You need to use the ".SourceControl" property in the "Opening" event of the
MenuStrip.
In the MSDN doc I found the following example that seems to work in my
VS2005 version:

Jean Lussier
Montreal (Canada)

---------------------------------------------------------------------------­-----------
Sub cms_Opening(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs)

        ' Acquire references to the owning control and item.
        Dim c As Control = fruitContextMenuStrip.SourceControl
        Dim tsi As ToolStripDropDownItem = fruitContextMenuStrip.OwnerItem

        ' Clear the ContextMenuStrip control's
        ' Items collection.
        fruitContextMenuStrip.Items.Clear()

        ' Check the source control first.
        If Not (c Is Nothing) Then
            ' Add custom item (Form)
            fruitContextMenuStrip.Items.Add(("Source: " +
c.GetType().ToString()))
        ElseIf Not (tsi Is Nothing) Then
            ' Add custom item (ToolStripDropDownButton or ToolStripMenuItem)
            fruitContextMenuStrip.Items.Add(("Source: " +
tsi.GetType().ToString()))
        End If

        ' Populate the ContextMenuStrip control with its default items.
        fruitContextMenuStrip.Items.Add("-")
        fruitContextMenuStrip.Items.Add("Apples")
        fruitContextMenuStrip.Items.Add("Oranges")
        fruitContextMenuStrip.Items.Add("Pears")

        ' Set Cancel to false.
        ' It is optimized to true based on empty entry.
        e.Cancel = False
    End Sub







- Show quoted text -

Thanks. Just what I needed.
 
Back
Top