Question

I have a ToolStripMenu with several ToolStripDropDownButtons. Those dropDownButtons have ToolStripMenuItems on themselves (sub buttons). I need to set visibility permission and the issue is that a user may have permissions only for some tsMenuItems or may be omitted from seeing all the item in a certain dropDownButton then the whole ToolStripDropDownButtons should be set to Visible = false.

The rights for the visibility are set in a public Enum like this:

[EnumValue("Clients")]
Clients = 1,

[EnumValue("Materials")]
Materials = 2,

[EnumValue("Uppers")]

and so on...

I started to write a method but the the logic it is based on is that each ToolStripItem is named just like the EnumValue. So what I need (if possible) is somehow to do that:

private void SetToolStripDropDownVisibility(ToolStripDropDown mainBtn, params ToolStripItem[] item)
        { 
            foreach (ToolStripItem tempItem in item)
            {
                EnumValue eValue = tempItem.Text;
                if (Helpers.GrantActivity(ControlEnum.eValue, ActionEnum.ShowMenuItem))
            }
        }
  • First if possible is to use tempItem.Text as EnumValue what I tried here, but obviously need some casting or other - EnumValue eValue = tempItem.Text; and the call the helper method with a correct argument - ControlEnum.eValue which as it seems to me still depends on that if I can use tempItem.Text as EnumValue.
Was it helpful?

Solution

I have the string (from the tempItem.Text) I need to use it as an EnumValue (from comments)

You can use Enum.Parse or Enum.TryParse<TEnum> method something like:

EnumValue enumValue;
if (Enum.TryParse<EnumValue>("Materials", out enumValue))
{
    //parsing successful
}
else
{
    //parisng failed. 
}

output:

enumValue = Materials

If your enum is defined as:

public enum EnumValue
{
    Clients = 1,
    Materials = 2,
}

OTHER TIPS

Try This, I have something similar in a project I did a few months back, it worked for me.

EnumValue enm = (EnumValue)Enum.Parse(typeof(EnumValue), tempItem.Text);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top