Resolved Get text of specific ToolStrip menu item

MPIon

Well-known member
Joined
Jun 13, 2020
Messages
73
Location
England
Programming Experience
10+
I am working with WinForms, Net 7, c# 11

I have a ToolStripMenuItem with several drop down items. e.g.
C#:
     Main Menu Item
            Drop Down Item 1
            Drop Down Item 2
            etc
All I want to do is get the text shown on one of the drop down menu items. e.g. if I asked for item 2, it would give me "Drop Down Item 2"

I am sure this must be simple, but there does not seem to be a suitable property available. I have searched the internet and can't find a solution, nor is it fathomable in the Microsoft documentation.

p.s. When I want this, all I know is the index of the item, not it's name. I could do something simple like storing the text of the current item and using that,
but I thought this would be a more elegant solution if I can find one.
 
Last edited by a moderator:
I still need to find the name of the menu item from the c# code using the index from the enum.
You almost certainly don't. You have the menu item itself when it's clicked. What do you think you need the name for?
Don't care about data models or views. As far as I am concerned, its all one integrated project, front end and back end.
You should get over that as soon as you can. You have no idea how many convoluted problems we have to try to solve because people go in with that attitude and end up painting themselves into a corner.
 
As far as I am concerned, its all one integrated project, front end and back end.

Otherwise known as "Spaghetti code".

Given a choice, I would rather deal with "baklava code" than "spaghetti code". :)
 
You have the menu item itself when it's clicked.

From the OP's post #2:
Yes, but unfortunately, I do not have the sender. I am trying to find the Text from a totally different part of the code, not in response to a click event on the "Main Menu Item" toolbar.

What do you think you need the name for?

Just my suspicion, but I think that the OP thought that they needed to access the item directly via variable name: _tsdpiDropDownItem1, or _tsdpiDropDownItem7. It's why they were trying to learn how to index into the menu, and as we've told them the yes they can index into the menu item collection.

The bigger mystery though is this: Why does the OP even need to get the text from the menu item? They also said in another post that they know exactly what item is at what position and what text is within that item. If they already know, then why pull the value from the View?
Yes, I did populate the items and I do know exactly what is in them and I know what is at any particular index.
 
You almost certainly don't. You have the menu item itself when it's clicked. What do you think you need the name for?

You should get over that as soon as you can. You have no idea how many convoluted problems we have to try to solve because people go in with that attitude and end up painting themselves into a corner.
No, you are wrong on this and don't understand my application. I do need the menu item name. I am not clicking on the item, I am retrieving the item name from its index in the drop down items. The name is then being shown elsewhere.
Regarding Models and Views, in my application, the Menu Items are part of the Model and the names don't exists anywhere else but in the Menu Item, so if I want them in the c# code, I have to get them directly from the Menu Item.

On the second point and comments from others, my code is not "spaghetti code" - I spend a long time removing any spaghetti from my code - as I did for years in my professional career as a programmer, probably before most people on this thread were even born.

Anyway, lets leave this conversation here, as it could get ugly.

But thanks for all the helpful comments and suggestions that have been posted - much appreciated.

p.s. mostly when people post, they are looking for answers as to HOW to do something, not WHY they should not be doing what they are trying to do.
 
mostly when people post, they are looking for answers as to HOW to do something, not WHY they should not be doing what they are trying to do.
Mostly, when people answer, they want to avoid more problems in the future, for themselves and for those asking the questions, so they'd like to make sure that you're doing the right things, in which case the why matters. We all volunteer here so we're not obliged to help anyone. I get nothing out of helping someone to do the wrong thing as I feel like it's a disservice to them and to programming in general. I'm not going to tell you how to do something if that something is bad.
 
Regarding Models and Views, in my application, the Menu Items are part of the Model and the names don't exists anywhere else but in the Menu Item

Perhaps the sensible engineering opportunity is to remove that. If you have a Model that contains the ids, names, display order etc, then you can

a) programmatically generate menu items from it and
b) use it as a lookup when you have X or Y and want the paired Y or X (or Z)
 
Back
Top Bottom