UserControl with a context menu

false74

Well-known member
Joined
Aug 4, 2010
Messages
76
Programming Experience
Beginner
I have created a new component which inherits from the UserControl class so it already has a ContextMenuStrip property inherited. However since my component consists of a textbox and two buttons, how can I make this contextmenu show when anything inside the component in clicked (show the menu when I right click the buttons or the textbox). Currently It will bring up the contextmenu of the control which I have clicked. So if I click the textbox I get the native Cut, Copy, Paste menu instead of the one I initialized in my component class.
I have also tried this:
VB.NET:
    Private Sub ShowContextMenu(ByVal sender As Object, ByVal e As MouseEventArgs) Me.MouseClick
        If e.Button = Windows.Forms.MouseButtons.Right Then
            If Me.ContextMenuStrip IsNot Nothing Then
                Me.ContextMenuStrip.Show(Me, e.Location)
            End If
        End If
    End Sub

But does not work as the class consists of multiple components (textbox and two buttons).
 
The UserControl (in fact, every control) has a ContextMenuStripChanged event. You would handle that event and, in the event handler, assign the UserControl's ContextMenuStrip to the ContextMenuStrip property of the child controls.
 
If I have read your response correctly I will have to disagree with your solution. According to your solution you want me to reassign the child controls' ContextMenuStrip to the parent UserControl class's ContextMenuStrip when the ContextMenuStripChanged event is raised. However when I do this I still have the same issue as before.

Essentially I have created my own Numeric Up/Down component (a textbox and two buttons). However I need to be able to assign a contextmenu to this component that can be accessed by both the textbox and buttons. Now the two buttons are in their own component so the NumericUpDown is just a textbox and a updownarrow component, which isn't important at this time but seems relevant to my issue.

When I create my UserControl class obviously I declare the textbox (box) and updown arrows (arrows) and position them and all that nonsense, so my ContextMenuStripChanged event looks like this:
VB.NET:
    Private Sub UpdateContextMenu() Handles Me.ContextMenuStripChanged
        box.ContextMenuStrip = Me.ContextMenuStrip
        arrows.ContextMenuStrip = Me.ContextMenuStrip
    End Sub

Now when I create my NumericUpDown object in my application and assign a new ContextMenuStrip to it I still have the same issue. Where the textbox still shows it's native Copy, Paste, etc menu and the buttons do not do anything (i have the same ContextMenuStripChanged event handler in the updownarrow control so that's not the issue there).
 
Works perfectly fine for me, so I can only assume that you have done something wrong. Here's how I tested:

1. Create a new WinForms project.
2. Add a User Control.
3. Add a Button and a TextBox to the user control.
4. Add the following code to the user control:


5. Build the project.
6. Add an instance of the user control to the form.
7. Add a ContextMenuStrip to the form.
8. Add some items to the ContextMenuStrip.
9. Assign the ContextMenuStrip object to the ContextMenuStrip property of the user control.
10. Run the project.

Now, if you right-click the user control, the Button or the TextBox, you should see the same menu that you added.
 
Sorry to butt in but I agree, everything worked fine for me too. I either assigned the property of the TextBox directly or assigned the ContextMenuStrip in the form_load event.
 
Back
Top