468,765 Members | 1,153 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,765 developers. It's quick & easy.

MenuItem Name property

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

Nov 15 '05 #1
9 3467
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?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Rakesh" <an*******@discussions.microsoft.com> wrote in message
news:0b****************************@phx.gbl...
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

Nov 15 '05 #2
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.
Nov 15 '05 #3
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
--
- Nicholas Paldino [.NET/C# MVP
- mv*@spam.guard.caspershouse.co

"Rakesh" <an*******@discussions.microsoft.com> wrote in messag
news:0b****************************@phx.gbl..
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-tim
right
Thanks in advanc

Rakes

Nov 15 '05 #4
to***********@gmx.de (Torsten Kraus) wrote in message news:<eb**************************@posting.google. com>...
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.Bi ndingFlags.NonPublic|System.Reflection.BindingFlag s.Public|System.Reflection.BindingFlags.Static|Sys tem.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
Nov 16 '05 #5
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
Nov 16 '05 #6
to***********@gmx.de (Torsten Kraus) wrote in message news:<eb**************************@posting.google. com>...
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.Bi ndingFlags.NonPublic|System.Reflection.BindingFlag s.Public|System.Reflection.BindingFlags.Static|Sys tem.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
Nov 16 '05 #7
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
Nov 16 '05 #8
to***********@gmx.de (Torsten Kraus) wrote in message news:<eb**************************@posting.google. com>...
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.Bi ndingFlags.NonPublic|System.Reflection.BindingFlag s.Public|System.Reflection.BindingFlags.Static|Sys tem.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
Nov 16 '05 #9
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
Nov 16 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Claus Holm | last post: by
4 posts views Thread by Jeff Mason | last post: by
6 posts views Thread by Agnes | last post: by
3 posts views Thread by Jason Huang | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.