Tuesday, July 17, 2018

c# - display int value from enum




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
}
}

Answer



To get all values of the Enum try this




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


and your totalSteps property should look like this



public int[] totalSteps
{
get { return Enum.GetValues(typeof(TotalSteps)).Cast().ToArray(); }
}


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...