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:
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:
Please help!
Pettrer
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
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: