0

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.
Leron
  • 9,546
  • 35
  • 156
  • 257
  • is that what you are looking for ? http://stackoverflow.com/questions/12403065/how-can-i-assign-a-string-to-an-enum-instead-of-an-intereger-value-in-c/12403191#12403191 – Habib Mar 28 '13 at 09:29
  • To be honest I don't fully understand your code but I think I need exactly the opposite - I have the string (from the `tempItem.Text`) I need to use it as an `EnumValue`. – Leron Mar 28 '13 at 09:35
  • 1
    can't you use [Enum.Parse](http://msdn.microsoft.com/en-us/library/essfb559.aspx) – Habib Mar 28 '13 at 09:38
  • Here's Jon Skeet's answer on similar question: http://stackoverflow.com/a/1033269/694852 – Artemix Mar 28 '13 at 09:39
  • I just find out that this exists. But looks like this is what I need. Still have to write some code. But if you want post it as answer. I think I'm gonna use it. – Leron Mar 28 '13 at 09:40
  • @Habib "That will do it for the name, not the description attribute." (C) Jon Skeet :) – Artemix Mar 28 '13 at 09:40
  • 1
    @Artemix, there is no description attribute on the Enum, I believe the OP is trying to parse Enum from string. – Habib Mar 28 '13 at 09:44
  • @Leron, better if you use `Enum.TryParse` check the answer – Habib Mar 28 '13 at 09:51

2 Answers2

1

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,
}
Habib
  • 219,104
  • 29
  • 407
  • 436
0

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);
Derek
  • 8,300
  • 12
  • 56
  • 88