Set enabled property of private field by name?

G

Guest

I am trying to locate a field (ToolStripMenuItem) by name in the current form
and set its Enabled property, but I'm having no luck (skill) so far.

Any help you could provide wold be greatly appreciated.

This is the code I have so far. The line using (ToolStripMenuItem field =
GetField(name)) is where I need to grab a reference to the field to affect,
and obviously the existing code is not correct, but it should give you the
idea.

private void SetSecurityOnMenu()
{
string name = String.Empty;
Constants.GUIObjectRestrictions restriction =
Constants.GUIObjectRestrictions.None;

statusStrip1.Items[0].Text = "Setting security on menu items...";

// Get the list of objects on this form that have restrictions
ArrayList l =
BL.SecuredGUIObjectRestrictions.GetSecuredGUIObjectRestrictionsForParent(this.Name, Common.CommaDelimitedSecurityGroupList(MyPrincipal));

for (int i = 0, limit = l.Count; i < limit; i++)
{
name = ((GUIObjectRestrictions)l).ObjectName;
restriction = ((GUIObjectRestrictions)l).Restriction;
using (ToolStripMenuItem field = GetField(name))
{
if (restriction == Constants.GUIObjectRestrictions.Disable)
{
field.Enabled = false;
}
}
}
statusStrip1.Items[0].Text = String.Empty;
}
 
G

Guest

That's actually the path I started down, but I was having trouble with it,
mainly because of the recursion required with submenus. Then I decided that
since the menus only had to be set up once and the disabled items would be
the exception it might just be faster to hit the menu item fields actually
needed to be disabled rather than traversing all of them.

Peter Ritchie said:
Why not just traverse the Items property of the MenuStip to which the menu
item(s) is associated with?

--
http://www.peterRitchie.com/


Byron said:
I am trying to locate a field (ToolStripMenuItem) by name in the current form
and set its Enabled property, but I'm having no luck (skill) so far.

Any help you could provide wold be greatly appreciated.

This is the code I have so far. The line using (ToolStripMenuItem field =
GetField(name)) is where I need to grab a reference to the field to affect,
and obviously the existing code is not correct, but it should give you the
idea.

private void SetSecurityOnMenu()
{
string name = String.Empty;
Constants.GUIObjectRestrictions restriction =
Constants.GUIObjectRestrictions.None;

statusStrip1.Items[0].Text = "Setting security on menu items...";

// Get the list of objects on this form that have restrictions
ArrayList l =
BL.SecuredGUIObjectRestrictions.GetSecuredGUIObjectRestrictionsForParent(this.Name, Common.CommaDelimitedSecurityGroupList(MyPrincipal));

for (int i = 0, limit = l.Count; i < limit; i++)
{
name = ((GUIObjectRestrictions)l).ObjectName;
restriction = ((GUIObjectRestrictions)l).Restriction;
using (ToolStripMenuItem field = GetField(name))
{
if (restriction == Constants.GUIObjectRestrictions.Disable)
{
field.Enabled = false;
}
}
}
statusStrip1.Items[0].Text = String.Empty;
}
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,


Unless you have an unusual amount of menues I would go this way. This
method will be good even if at some point you decide to add menuitems by
code.


--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Byron said:
That's actually the path I started down, but I was having trouble with it,
mainly because of the recursion required with submenus. Then I decided
that
since the menus only had to be set up once and the disabled items would be
the exception it might just be faster to hit the menu item fields actually
needed to be disabled rather than traversing all of them.

Peter Ritchie said:
Why not just traverse the Items property of the MenuStip to which the
menu
item(s) is associated with?

--
http://www.peterRitchie.com/


Byron said:
I am trying to locate a field (ToolStripMenuItem) by name in the
current form
and set its Enabled property, but I'm having no luck (skill) so far.

Any help you could provide wold be greatly appreciated.

This is the code I have so far. The line using (ToolStripMenuItem
field =
GetField(name)) is where I need to grab a reference to the field to
affect,
and obviously the existing code is not correct, but it should give you
the
idea.

private void SetSecurityOnMenu()
{
string name = String.Empty;
Constants.GUIObjectRestrictions restriction =
Constants.GUIObjectRestrictions.None;

statusStrip1.Items[0].Text = "Setting security on menu items...";

// Get the list of objects on this form that have restrictions
ArrayList l =
BL.SecuredGUIObjectRestrictions.GetSecuredGUIObjectRestrictionsForParent(this.Name,
Common.CommaDelimitedSecurityGroupList(MyPrincipal));

for (int i = 0, limit = l.Count; i < limit; i++)
{
name = ((GUIObjectRestrictions)l).ObjectName;
restriction = ((GUIObjectRestrictions)l).Restriction;
using (ToolStripMenuItem field = GetField(name))
{
if (restriction ==
Constants.GUIObjectRestrictions.Disable)
{
field.Enabled = false;
}
}
}
statusStrip1.Items[0].Text = String.Empty;
}
 
Top