8

I have a propertygrid which I need to create a combobox inside the propertygrid and display int value (1 to 9), I found using enum is the easiest way, but enum couldn't display int value, even I try to cast it to int, but I do not know how to return all the value. Any other way to do this? Thanks in Advance. Below is my code.

public class StepMode
    {
        private TotalSteps totalSteps;

        public TotalSteps totalsteps
        {
            get { return totalSteps; }
            set { value = totalSteps; }
        }
        public enum TotalSteps
        {
            First = 1,
            Second = 2,
            Three = 3,
            Four = 4,
            Five = 5,
            Six = 6,
            Seven = 7,
            Eight = 8,
            Nine = 9
        }
    }
Mers Tho
  • 149
  • 1
  • 9

3 Answers3

4

To get all values of the Enum try this

var allValues = Enum.GetValues(typeof(TotalSteps)).Cast<int>().ToArray();

and your totalSteps property should look like this

public int[] totalSteps
{
   get { return Enum.GetValues(typeof(TotalSteps)).Cast<int>().ToArray(); }
}
Ramin
  • 197
  • 8
  • It is still not working, it shows, "Cannot implicitly convert type"System.Collections.Generic.IEnumerable to StepMode.TotalSteps" – Mers Tho May 10 '16 at 02:38
  • The `allValues` variable would be an IEnumerable because it contains integer list of all Enum values so you need to change your totalSteps DataType from TotalSteps to IEnumerable – Ramin May 10 '16 at 02:43
  • check the edit please I have changed the getter as well. – Ramin May 10 '16 at 02:46
  • I've edited my code, it doesn't show error, but in the propertygrid, the value shows "WpfApplication18.Configuration+StepMode+TotalSteps[]" in textbox, instead of the int value (1 to 9) in combobox. – Mers Tho May 10 '16 at 02:53
  • Can we see your code on how do you set these values to your combobox – Ramin May 10 '16 at 02:54
  • I just have a enum class in Configuration class, and then propertygrid1.selectedobject = configuration. that's all, it automatically to show as combobox in propertygrid if using enum. – Mers Tho May 10 '16 at 02:59
  • I think the selectedobject property of PropertyGrid accepts array so better to change the IEnumerable to an Array of int. See the edit please – Ramin May 10 '16 at 03:06
  • yes, thank you. but this time it shows System.Int32[] – Mers Tho May 10 '16 at 03:52
0

how can I return it in "get" function? the value cannot be converted

Property (I guess) is defined to get/set selected enum value, so you can't return an int when type is TotalSteps.

I suggest have another readonly property inside Step which converts selected enum value to an int

 public int Step
 {
     get {return (int)totalsteps; }
 }

Since you mentioned (as a comment) you want all int values to bind to ComboBox do this.

 public List<int< ComboValues
 {
     get { return Enum.GetValues(typeof(TotalSteps)).Cast<int>().ToList(); } 
 }
Hari Prasad
  • 16,716
  • 4
  • 21
  • 35
0

you should make a property which return an int instead of TotalSteps

u are doing this

    private TotalSteps totalSteps;

    public TotalSteps totalsteps
    {
        get { return totalSteps; }
        set { value = totalSteps; }
    }

and I would suggest to do this

    private TotalSteps totalSteps;
    private int totalStepsInInt;

    public int TotalstepsInInt
    {
        get { return totalStepsInInt; }
        set { totalStepsInInt = value; }
    }

and while setting this property u have to convert totalSteps in int by doing this.

   `TotalStepsInInt = (int)totalSteps;`
Amit
  • 1,821
  • 1
  • 17
  • 30
  • and if you want a array or list of all values (of course in integer), you may loop this logic for all values in enumeration. [link]http://stackoverflow.com/questions/972307/can-you-loop-through-all-enum-values – Amit May 10 '16 at 03:14