Dynamically adding menu items and their OnClick events

  • Thread starter Thread starter Anil Gupte/iCinema.com
  • Start date Start date
A

Anil Gupte/iCinema.com

How do I add items to a menu while simultanesously adding an OnClick event
for each. I have an 2Xn array of text items (labels) and matching URLs.e.g.

"Google", www.google.com
"Yahoo", www.yahoo.com

I want the first column to be the menu label and the second column is where
the browser goes when the menu is clicked. OK, so the first part is easy.

AdditionalToolStripMenu.DropDownItems.Add(strResearchLinksArray(i, 1))

How do I go to strResearchLinksArray(i, 0) when that item is clicked. I am
vb.net 2005.
 
Am 02.03.2010 17:38, schrieb Anil Gupte/iCinema.com:
How do I add items to a menu while simultanesously adding an OnClick event
for each. I have an 2Xn array of text items (labels) and matching URLs.e.g.

"Google", www.google.com
"Yahoo", www.yahoo.com

I want the first column to be the menu label and the second column is where
the browser goes when the menu is clicked. OK, so the first part is easy.

AdditionalToolStripMenu.DropDownItems.Add(strResearchLinksArray(i, 1))

How do I go to strResearchLinksArray(i, 0) when that item is clicked. I am
vb.net 2005.

dim item = AdditionalToolStripMenu.DropDownItems.Add(strResearchLinksArray(i, 1))
item.tag = i

In the click event:

Dim index = DirectCast(DirectCast(sender, ToolStripItem).Tag, Integer)
dim address = strResearchLinksArray(index, 0)


- or -

Create a class inheriting from ToolStripMenuItem and add a property "ServerName".
 
Sorry, I have been unable to connect for a while. Thanx for your help.

I am not sure I understand your solution. You said "in the click event"
and you have me add a line. My question is, how does one add or assign a
click event to each item in the array?
 
Am 07.03.2010 01:02, schrieb Anil Gupte/iCinema.com:
Sorry, I have been unable to connect for a while. Thanx for your help.

I am not sure I understand your solution. You said "in the click event"
and you have me add a line. My question is, how does one add or assign a
click event to each item in the array?

Sorry, only the last question ("How do I go to strResearchLinksArray...")
stayed in my mind. :-) Because you said...

....I assumed that adding the items already works. To have a procedure handle
an event, use the Addhandler statement:
dim item =
AdditionalToolStripMenu.DropDownItems.Add(strResearchLinksArray(i, 1))

AddHandler item.Click, AddressOf OnMenuItemClick

'...

Sub OnMenuItemClick(ByVal sender As Object, ByVal e As System.EventArgs)
'code from last post
End Sub
 
Back
Top