contextmenustrip variables

pettrer

Well-known member
Joined
Sep 5, 2008
Messages
92
Programming Experience
10+
Hi,

I need help with the contextmenustrip control.

I have a Winforms application in VB.Net. I'm trying to accomplish this:

1. Right-click a label that has a person's name.
2. This label should have a contextmenustrip attached to it.
3. The contextmenustrip should be in a different file (in order to be accessed from all forms in my application).
4. The user should be able to send a variable so that an appropriate action is taken.

For example: I have some persons in a datagridview. I click one of them to get that person's details on a couple of labels. When I right-click the name label, a contextual menu pops up, letting me copy the person's email address, creating an email (using Outlook or the like), etc.

I have achieved this functionality when I have everything in one file and a designer-made contextmenustrip, but the main problem is sending the variable when I put the code in another class.

Here are my two (great!) subs for copying to clipboard / creating an email message, using stored procedures:

VB.NET:
Private Sub KopieraNamnPersonnnrEpostToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles KopieraNamnPersonnrEpostToolStripMenuItem.Click
        Dim id As Long = DGV1.SelectedRows(0).Cells(COL_1).Value
        Dim arr As Array = funGetArray("fv_studentinfo " & id)
        Clipboard.SetDataObject(arr(1, 0) + " " + arr(2, 0) + " " + (arr(3, 0).ToString.Substring(0, 6) + "-" + arr(3, 0).ToString.Substring(arr(3, 0).ToString.Length - 4, 4)) _
        & " " + arr(4, 0) + " " + arr(5, 0) + " " + arr(6, 0) + " " + arr(7, 0) + " " + arr(8, 0))
    End Sub

    Private Sub SkapaEpostmeddelandeToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SkapaEpostmeddelandeToolStripMenuItem.Click
        Dim id As Long = DGV1.SelectedRows(0).Cells(COL_1).Value
        Dim arr As Array = funGetArray("fv_studentinfo " & id)
        Process.Start("Mailto:" & arr(8, 0))
    End Sub
So, if I put this code in another sub, how do I pass the id value along? It's not the sourcecontrol value, or so I believe.

I have struggled for quite some time with this now but the msdn examples are filled with nonsense (tomatoes and whatever!).

I thought that I'd be able to pass the variable to the subs from the instatiated contextmenustrip in the common sub, but it doesn't seem possible to me. Otherwise, this would nice:

VB.NET:
Sub cmPerson(ByVal id As Long)
'Should be called like this:
        'lblcNotering.ContextMenu = modCommon.cmPerson(DGV1.SelectedRows(0).Cells(COL_1).Value)
    
       Dim cmPerson As System.Windows.Forms.ContextMenu
        cmPerson = New System.Windows.Forms.ContextMenu()
        Dim menuItem1 As System.Windows.Forms.MenuItem
        menuItem1 = New System.Windows.Forms.MenuItem()
        Dim menuItem2 As System.Windows.Forms.MenuItem
        menuItem2 = New System.Windows.Forms.MenuItem()
        Dim menuItem3 As System.Windows.Forms.MenuItem
        menuItem3 = New System.Windows.Forms.MenuItem()
        Dim menuItem4 As System.Windows.Forms.MenuItem
        menuItem4 = New System.Windows.Forms.MenuItem()

        cmPerson.MenuItems.AddRange(New System.Windows.Forms.MenuItem(id) {menuItem1, menuItem2, menuItem3, menuItem4})
        menuItem1.Index = 0
        menuItem1.Text = "Kopiera namn"
        menuItem2.Index = 1
        menuItem2.Text = "Kopiera personnummer"
        menuItem3.Index = 2
        menuItem3.Text = "Kopiera epostadress"
        menuItem3.Index = 3
        menuItem3.Text = "Kopiera namn, personnr, adress mm"
        menuItem3.Index = 4
        menuItem3.Text = "Skapa e-postmeddelande..."

'What I'm looking for if it exists (or a couple of if statements)...
menuitem3.action = SkapaEpostmeddelandeToolStripMenuItem.Click(id)
        End Sub

Please help!

Pettrer
 
Last edited by a moderator:
So, if I put this code in another sub, how do I pass the id value along? It's not the sourcecontrol value, or so I believe.
You have to go via the Label (SourceControl). If the Label is bound you could reference its datasource, if not and Label is manually set you can assign the id to the Label.Tag.
 
John,

Thanks for replying. The problem is that a label doesn't have an ID property (at least not as according to Visual Studio 2008):

Error 21 'id' is not a member of 'System.Windows.Forms.Label'.

Given the magnitude of your VB.Net knowledge, am I not understanding you correctly? Also, am I making this unnecessarily difficult? For some reason, its been very hard to find info on this kind of feature.

Thanks again,

Pettrer
 
The Label has a .Tag Property that is what JohnH is referring to. You can place your id value in the .Tag property. It has a value of type System.Object so it can be anything you like.
 
Hello again,

I've now successfully created a user control, which I can add to my own project. However, I have a small problem. The sender in the contextmenustrip is the contextmenustrip's ITEM, that is, unfortunately NOT the LABEL that made the contextmenustrip appear. So how do I pass the value from the label? Note that the label could have any name, so I can't use just "myLabel.Tag".

I'm looking for a piece of code that I can put in the Load event of the user control, something like this:

id = Parent.clickedControl.Tag (where id is a private variable declared at the top of the user control's code. Note that I don't know if parent has anything to do with this).

...or something else, if there is nothing like what I proposed.

Thanks again for all help!

Pettrer
 
Last edited:
Of course the sender of the menu event is the menu/item, the sender is always the instance of the class that raised the event. What you need to get is the given SourceControl.
 
Back
Top