Nov 3, 2008

HatchStyle Enum in a combo box

I wanted to use the HatchStyle enum in a combo box. No problem right, I just set the data source like this:
cmbHatchTypes.DataSource = Enum.GetValues(typeof(HatchStyle));
Well, not so fast. For reasons I can't begin to fathom (and maybe someone out there will enlighten me), when you do this you get a list that has duplicates of a few of the hatch styles. Even that would be OK if the duplicates actually existed in the enumerated list but they don't. They just cause there to be a mismatch between the combo pop up item that is displayed and the selected index into the enum. Very annoying.
To work around this I used the following brute force work-around and if anyone has a more elegant approach I'm all ears.
Array hatch_array = Enum.GetValues(typeo(HatchStyle));
List hatch_list = new Listhatch_array.Length);
foreach (object str in hatch_array)
{
   hatch_list.Add(str.ToString());
}
hatch_list = (from string h in hatch_list
              select h).Distinct().ToList();

cmbHatchTypes.DataSource = hatch_list

About Me

My photo
Tod Gentille (@todgentille) is now a Curriculum Director for Pluralsight. He's been programming professionally since well before you were born and was a software consultant for most of his career. He's also a father, husband, drummer, and windsurfer. He wants to be a guitar player but he just hasn't got the chops for it.