By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,258 Members | 1,679 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,258 IT Pros & Developers. It's quick & easy.

Share context menu for multiple controls

P: 3
This is a conceptual question, so I'm not inluding code yet. I can't understand how to share a context menu strip between controls - how do I know which control the user was in when he right clicked it and made a selection?

Example: I have 3 text boxes that all use the same contextmenustrip and require the exact same functionality so no need to load new options. The user selects one of the options and an event fires something like this:

Private Sub mnuX_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuX.Click

So far, I can't figure out how to determine which text box is responsible for it??? Help please, Ed
Dec 2 '10 #1
Share this Question
Share on Google+
1 Reply

P: 3
I found the solution, using the SourceControl property of the menu itself - not in any of the passed parameters. Here is some interesting sample code:

Private Sub ContextMenuStrip1_Opening(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles ContextMenuStrip1.Opening
If ContextMenuStrip1.SourceControl Is TextBox1 Then
MsgBox("using TextBox1")
ElseIf ContextMenuStrip1.SourceControl Is TextBox2 Then
MsgBox("using TextBox2")
ElseIf ContextMenuStrip1.SourceControl Is ListBox1 Then
MsgBox("using ListBox1")
End If
End Sub

Private Sub Opt1MenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Opt1MenuItem.Click
If TypeOf ContextMenuStrip1.SourceControl Is TextBox Then
Dim txtbx As TextBox = ContextMenuStrip1.SourceControl

MsgBox("Opt1, " + txtbx.Text)
ElseIf TypeOf ContextMenuStrip1.SourceControl Is ListBox Then
Dim lstbx As ListBox = ContextMenuStrip1.SourceControl

MsgBox("Opt1, " + lstbx.SelectedItem.ToString())
End If
End Sub
Dec 2 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.