display int value from enum [duplicate]
Asked Answered
C

3

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
        }
    }
Clisthenes answered 10/5, 2016 at 2:4 Comment(3)
You've tried int steps = (int)TotalSteps.First?Backstroke
yes @Backstroke it will workIlluminati
@Backstroke thank you for answering, and yes, I've tried, but how can I return it in "get" function? the value cannot be convertedClisthenes
L
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(); }
}
Larousse answered 10/5, 2016 at 2:23 Comment(8)
It is still not working, it shows, "Cannot implicitly convert type"System.Collections.Generic.IEnumerable<int> to StepMode.TotalSteps"Clisthenes
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>Larousse
check the edit please I have changed the getter as well.Larousse
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.Clisthenes
Can we see your code on how do you set these values to your comboboxLarousse
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.Clisthenes
I think the selectedobject property of PropertyGrid accepts array so better to change the IEnumerable<int> to an Array of int. See the edit pleaseLarousse
yes, thank you. but this time it shows System.Int32[]Clisthenes
B
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(); } 
 }
Bistre answered 10/5, 2016 at 2:33 Comment(2)
Thank you for answering, this is also not working, it shows zero in textbox, instead of a set of int value in a combobox.Clisthenes
Ohh.. ok, I got what you are saying. Give me a minute I'll update it.Bistre
M
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;`
Misanthrope answered 10/5, 2016 at 3:11 Comment(1)
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]#972807Misanthrope

© 2022 - 2024 — McMap. All rights reserved.