Visual Basic .NET Forums  
Click here to advertise with us

Go Back   Visual Basic .NET Forums > Components & Controls > Menus & Toolbars

Menus & Toolbars Discussion of menu and toolbar components

VB.NET Forums Newsletter Signup:
Email address:


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-06-2008, 11:57 AM
VB.NET Forum Enthusiast
.NET Framework: .NET 3.5 (VS 2008)
 
Join Date: Sep 2008
Posts: 89
Reputation: 22
pettrer is on a distinguished programming path ahead
Default contextmenustrip variables

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:

Code:
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:

Code:
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 JohnH; 10-06-2008 at 12:11 PM. Reason: formatted post for readability
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 10-06-2008, 12:18 PM
JohnH's Avatar
VB.NET Forum Moderator
.NET Framework: .NET 3.5 (VS 2008)
 
Join Date: Dec 2005
Location: Norway
Age: 37
Posts: 10,325
Reputation: 1315
JohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond repute
Default

Quote:
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.
__________________
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 10-07-2008, 3:30 AM
VB.NET Forum Enthusiast
.NET Framework: .NET 3.5 (VS 2008)
 
Join Date: Sep 2008
Posts: 89
Reputation: 22
pettrer is on a distinguished programming path ahead
Post

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 10-07-2008, 6:21 AM
vis781's Avatar
VB.NET Forum Moderator
.NET Framework: .NET 2.0 (VS 2005)
 
Join Date: Aug 2005
Location: Cambridge, UK
Age: 31
Posts: 2,016
Reputation: 211
vis781 I'm the one to askvis781 I'm the one to askvis781 I'm the one to askvis781 I'm the one to askvis781 I'm the one to askvis781 I'm the one to ask
Default

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.
__________________
GDI+ Code Generation Utility : GDI+ Architect

A Must : FxCop .Net Code analyzer : FxCop
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 10-08-2008, 8:18 AM
VB.NET Forum Enthusiast
.NET Framework: .NET 3.5 (VS 2008)
 
Join Date: Sep 2008
Posts: 89
Reputation: 22
pettrer is on a distinguished programming path ahead
Default

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 by pettrer; 10-08-2008 at 9:06 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 10-08-2008, 9:40 AM
JohnH's Avatar
VB.NET Forum Moderator
.NET Framework: .NET 3.5 (VS 2008)
 
Join Date: Dec 2005
Location: Norway
Age: 37
Posts: 10,325
Reputation: 1315
JohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond repute
Default

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.
__________________
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 10-08-2008, 9:45 AM
VB.NET Forum Enthusiast
.NET Framework: .NET 3.5 (VS 2008)
 
Join Date: Sep 2008
Posts: 89
Reputation: 22
pettrer is on a distinguished programming path ahead
Default

Of course - stupid me! (I had already referred to it above...)

Thanks a lot!

Pettrer
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





All times are GMT -4. The time now is 7:20 AM.

Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2


For advertising opportunities click here.