Is there a way of getting the index of an item in a collection of ToolStripMenuItems?

A

Academia

I use to do this:

Dim index As Integer = CType(sender, MenuItem).Index

but ToolStripMenuItems do not have an Index property so I can set the Tag or
MergeIndex property of those items and use that.

But that is error prone since changes in the menu require that those values
be kept synchronized.

Even though there is no Index property I have the feeling that the
collection might have the equivalent.



Is there a way of getting the index of the item in the collection?





Thanks
 
A

Armin Zingler

Academia said:
I use to do this:

Dim index As Integer = CType(sender, MenuItem).Index

but ToolStripMenuItems do not have an Index property so I can set
the Tag or MergeIndex property of those items and use that.

But that is error prone since changes in the menu require that those
values be kept synchronized.

Even though there is no Index property I have the feeling that the
collection might have the equivalent.



Is there a way of getting the index of the item in the collection?

You should have mentioned the type of collection. Is it
ToolStripItemCollection? Why not look at the members available? I see
the IndexOf method. It probably returns the index of the item.


Armin
 
P

Patrice

As in most cases the overall goal could help (and which event are you
handling ?)

My first thought would be first to have the event handled by the specific
event handler tied to this control.

If you need something more generic, my personal preference is to use the
name or just test the object (If sender Is MyObject etc...) rather than the
index.

If you really need the index you'll find an IndexOf method on almost if not
all collections (get at the owner and use the IndexOf method on the
collection that contains these elements to get the index).
 
A

Academia

This is why the method is called:

For Each mI As ToolStripItem In Me.MenuItemGeneral_Tool.DropDownItems

AddHandler mI.Click, AddressOf HandlesTool_Click

Next mI



and this is how index is used

CType(MenuItemGeneral_Tool.DropDownItems(menuTag),
ToolStripMenuItem).Checked = True

===

the ToolStripMenuItem.CheckOnClick doesn't work for me because I don't want
it unchecked automatically.

thanks
 
S

Steve Gerrard

Academia said:
I use to do this:
Dim index As Integer = CType(sender, MenuItem).Index
and this is how index is used

CType(MenuItemGeneral_Tool.DropDownItems(menuTag),
ToolStripMenuItem).Checked = True

You are walking around the block to get next door. There is no need to take the
sender, find its index (or tag), then use that to retrieve the item from the
parent collection. You already have the item; just use it directly.

CType(sender, TooStripMenuItem).Checked = True
 
A

Academia

I ask sometimes because I want to be sure I'm not missing something simple.

Good thing I don't try to play chess.

thanks
 

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