Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

display enum values inside winforms combobox

Tags:

c#

.net

winforms

Let's say that I have following enum

public enum MyMode { A = 1, B = 2, C = 3, D = 4 };

and I want to use this enum as list of values inside combobox, I tried with

cmbMyMode.Items.Add(Enum.GetValues(typeof(MyMode )));

but I'm getting following

MyMode[] Array

I need to display A, B, C, D, and is it possible to use custom string instead of A,B,C,D

Thanks

like image 500
user1765862 Avatar asked Dec 02 '22 19:12

user1765862


1 Answers

List<MyMode> modes = Enum.GetValues(typeof(MyMode)).Cast<MyMode>().ToList();
cmbMyMode.DataSource = modes;

And to customize the labels:

var modes = Enum.GetValues(typeof(MyMode)).Cast<MyMode>().Select(mode => 
                   new { Value = mode, Title = string.Format("-->{0}<--", mode) }).
                 ToList();
cmbMyMode.ValueMember = "Value";
cmbMyMode.DisplayMember = "Title";
cmbMyMode.DataSource = modes;

and then

cmbMyMode.SelectedValue
like image 184
vc 74 Avatar answered Dec 15 '22 03:12

vc 74