Dynamically adding menu items and their OnClick events

  • Thread starter Anil Gupte/iCinema.com
  • 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.
 
A

Armin Zingler

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".
 
A

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?
 
A

Armin Zingler

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top