MenuItem Name property

  • Thread starter Thread starter Rakesh
  • Start date Start date
R

Rakesh

Hi,

I am able to obtain a MenuItem object's Name property @
design-time, but am not able to get the same @ run-
time...why?

And since MenuItem doesn't inherit from Control class,
it's not even supposed to be available @ design-time
right?

Thanks in advance
Rakesh
 
Rakesh,

The designer accomplishes the name property by actually extending your
class and then hosting that in the property grid. The Name property is then
expose off that derived class. The Name property is really the name of the
variable, which you should know already. If you don't, then you will need
to place the identifier of some sort on the MenuItem (by extending the
MenuItem class and attaching your information).

What are you trying to do?
 
I have the same problem

I am trying to build an MDI template other developers can customize later.
I want to include code on the mainform that will enable or disable menuitems added later given only their names. the function will be called from a child form. These menuitems do not exist at design time for the template
I have code that works using the text property but this does not allow for common names. E.g. File/new and Window/new both get disabled or enabled at the same time.
 
It sounds like I have a similar problem

I am working on an MDI template and I want to place code on the mainform to enable or disable menuitems.

The first problem is I must cope with menu items that do not exist while I am designing the template, they can be added later by another developer. I do not want to make the menu items public and open the existing items to tampering

The trigger to disable an item will originate from a child form which will not know about the menuitem objects and must pass a string to identify an item

I have code that works using the Text property but it won't allow for common item names. e.g. it thinks File/new and Window/new are the same thing

the second problem is processing the menu selections. If you want to process the menu click on the child form and cannot pass it an object, how do you identify the menu item? Remembering that the item in question was added to a derived form not the original template


----- Nicholas Paldino [.NET/C# MVP] wrote: ----

Rakesh

The designer accomplishes the name property by actually extending you
class and then hosting that in the property grid. The Name property is the
expose off that derived class. The Name property is really the name of th
variable, which you should know already. If you don't, then you will nee
to place the identifier of some sort on the MenuItem (by extending th
MenuItem class and attaching your information)

What are you trying to do
 
hi there,

i've had the same problem. Unfortunally MenuItem and ToolBarButton
have no Name property. But the name Property is only the Name of the
Variable/Field.
So you can figure out the Name of a ToolBarButton or MenuItem using
Reflection.

The following Sample retreive the Name of a given ToolBarButton:

public static string GetToolBarButtonName(System.Windows.Forms.Form
frmParent, System.Windows.Forms.ToolBarButton tbButton)
{
FieldInfo[] fields =
frmParent.GetType().GetFields(System.Reflection.BindingFlags.NonPublic|System.Reflection.BindingFlags.Public|System.Reflection.BindingFlags.Static|System.Reflection.BindingFlags.Instance);
foreach(FieldInfo field in fields)
{
if (field.GetValue(frmParent) == tbButton)
return field.Name;
}
return "";
}

ciao
Torsten
 
I do see the MenuItems and toolbarbuttons name properties. Am I missing
something ?
 
genc ymeri said:
I do see the MenuItems and toolbarbuttons name properties. Am I missing
something ?

You see (Name) for a MenuItem or a ToolBarButton in the Designer.
So it looks like a Property of those classes but it is'nt.

Try to access .Name for a MenuItem in your Code an you'll see.

ciao
Torsten
 
hi there,

i've had the same problem. Unfortunally MenuItem and ToolBarButton
have no Name property. But the name Property is only the Name of the
Variable/Field.
So you can figure out the Name of a ToolBarButton or MenuItem using
Reflection.

The following Sample retreive the Name of a given ToolBarButton:

public static string GetToolBarButtonName(System.Windows.Forms.Form
frmParent, System.Windows.Forms.ToolBarButton tbButton)
{
FieldInfo[] fields =
frmParent.GetType().GetFields(System.Reflection.BindingFlags.NonPublic|System.Reflection.BindingFlags.Public|System.Reflection.BindingFlags.Static|System.Reflection.BindingFlags.Instance);
foreach(FieldInfo field in fields)
{
if (field.GetValue(frmParent) == tbButton)
return field.Name;
}
return "";
}

ciao
Torsten

Hi

The code above works fine for Menu object, but not MenuItem object.

Any ideas?

Thx,

Carel
 
I've had the same problem as Rakesh

I've got a Menu in my application that will be changing from time to time as I revise the application throughout the remainder of it's Alpha stage, and possibly during it's Beta stage

I've got upwards of 50 MenuItems under one particular Menu selection. I'm using a try-catch-end try to catch several exceptions that are sometimes thrown. Instead of using 50+ subProcedure declarations, I did one declaration that handles all of the menuitems that I need. However, when I went to do a select case decision structure, I found out the hard way that .name is not a property, but a protected or private field that was never made accesible via a string readonly property. I think the .NET developers need to fix this, as MANY controls have a string readonly property for access at run time

Dolphin Incarnate
 
Back
Top