9

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
        }
    }
3
  • 3
    You've tried int steps = (int)TotalSteps.First? Commented May 10, 2016 at 2:06
  • yes @Kramb it will work Commented May 10, 2016 at 2:08
  • @Kramb thank you for answering, and yes, I've tried, but how can I return it in "get" function? the value cannot be converted Commented May 10, 2016 at 2:17

3 Answers 3

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(); }
}
Sign up to request clarification or add additional context in comments.

8 Comments

It is still not working, it shows, "Cannot implicitly convert type"System.Collections.Generic.IEnumerable<int> to StepMode.TotalSteps"
The allValues variable would be an IEnumerable<int> because it contains integer list of all Enum values so you need to change your totalSteps DataType from TotalSteps to IEnumerable<int>
check the edit please I have changed the getter as well.
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.
Can we see your code on how do you set these values to your combobox
|
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(); } 
 }

2 Comments

Thank you for answering, this is also not working, it shows zero in textbox, instead of a set of int value in a combobox.
Ohh.. ok, I got what you are saying. Give me a minute I'll update it.
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;`

1 Comment

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]stackoverflow.com/questions/972307/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.