Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
I have a context menu that is used on multiple controls on a form. It is pretty simple "boolean" selection menu (i.e. "True" or "False" conditions. Below is the click event for one of these (false):

Private Sub Bool_False_Click(sender As System.Object, e As System.EventArgs) Handles pmnuFilterBool_False.Click
    Dim iItem As ToolStripMenuItem = CType(sender, ToolStripMenuItem)
    Dim cms As ContextMenuStrip = CType(iItem.Owner, ContextMenuStrip)

    If cms.SourceControl IsNot Nothing Then
        Dim jName As String = cms.SourceControl.Name

        If jName <> "" Then
            Select Case jName
                Case Is = "ccbName"
                    ContainerCL.AddFieldVal("Name", ccbName.Text, False)
                Case Is = "cbUnit"
                    ContainerCL.AddFieldVal("Unit", cbUnit.Text, False)

            End Select

        Else
            Dim jTypeName As String = TypeName(cms.SourceControl)
            If jTypeName = "ToolStripComboBoxControl" Then
                Dim jItem As Control = (DirectCast(pmnuFilterBool.SourceControl, Control))

                'here is where I don't know what to do. How do I get the underlying (embedded) ToolStripComboBox?

            End If

        End If

    End If

End Sub


It works fine for context menu attached to standard controls like textbox or combobox. The trouble is in using items where the control is "embeded" (i.e. nested)

On the form I have a custom toolstripcombobox that also uses the contextmenu. It was developed with some help on CodeProject. See Disable default context menu[^]

The custom toolstripcombobox assigns the context menu strip to the underlying combobox.

Option Strict Off
Option Explicit On

<System.ComponentModel.Description("Custom Control to add a context menu to a toolstrip combobox")> _
Public Class MyToolStripComboBox
    Inherits Forms.ToolStripComboBox
    ' allows a context menu to be added to a toolstrip combobox control
    Public Property ContextMenuStrip() As ContextMenuStrip
        Get
            Return Me.ComboBox.ContextMenuStrip
        End Get
        Set(ByVal iConMenu As ContextMenuStrip)
            Me.ComboBox.ContextMenuStrip = iConMenu
        End Set
    End Property
End Class


So, in the click event on the parent form, how do we "drill down" to get the underlying ToolStripComboBox. Really what I want is it's name and the value (text) so I can take the appropriate action.

What I have tried:

This problem is apparently know in the .net programming community. See here .net - SourceControl of ContextMenuStrip is Nothing in ToolStripMenuItem Click? - Stack Overflow[^] for example. I think there is also a similar posting on CodeProject.

These solution and other similar ones did not solve the problem. I am not familiar enough with source control objects to work through this without some help. Hopefully this will be a learning experience for me.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900