Filling combobox with enumeration values

D

Dave

How would I go about filling a combo box with some existing enumeration
values? Can I bind it to the enumeration? Then, once the selection is made
from the combo box, set a property to the enumeration value?

Dave
 
H

Herfried K. Wagner [MVP]

Dave said:
How would I go about filling a combo box with some existing enumeration
values? Can I bind it to the enumeration? Then, once the selection is made
from the combo box, set a property to the enumeration value?

\\\
Private Sub Form1_Load( _
ByVal sender As Object, _
ByVal e As EventArgs) Handles MyBase.Load
Me.ListBox1.DataSource = _
[Enum].GetNames(GetType(KnownColor))
End Sub

Private Sub ListBox1_SelectedIndexChanged( _
ByVal sender As Object, _
ByVal e As EventArgs _
) Handles ListBox1.SelectedIndexChanged
Me.ListBox1.BackColor = _
Color.FromKnownColor( _
DirectCast( _
[Enum].Parse( _
GetType(KnownColor), _
CStr(ListBox1.SelectedItem) _
), _
KnownColor _
) _
)
End Sub
///
 
D

Dave

Thanks Herfried,

That will be very useful!!

Dave

Herfried K. Wagner said:
Dave said:
How would I go about filling a combo box with some existing enumeration
values? Can I bind it to the enumeration? Then, once the selection is
made from the combo box, set a property to the enumeration value?

\\\
Private Sub Form1_Load( _
ByVal sender As Object, _
ByVal e As EventArgs) Handles MyBase.Load
Me.ListBox1.DataSource = _
[Enum].GetNames(GetType(KnownColor))
End Sub

Private Sub ListBox1_SelectedIndexChanged( _
ByVal sender As Object, _
ByVal e As EventArgs _
) Handles ListBox1.SelectedIndexChanged
Me.ListBox1.BackColor = _
Color.FromKnownColor( _
DirectCast( _
[Enum].Parse( _
GetType(KnownColor), _
CStr(ListBox1.SelectedItem) _
), _
KnownColor _
) _
)
End Sub
///
 
J

Jay B. Harlow [MVP - Outlook]

Dave,
In addition to the other comments, I normally bind to the values of an Enum,
rather then the Names of an Enum.

Private Sub Form1_Load( _
ByVal sender As Object, _
ByVal e As EventArgs) Handles MyBase.Load
'Me.ListBox1.DataSource = _
' [Enum].GetValues(GetType(KnownColor))

Me.ListBox1.DataSource = _
[Enum].GetValues(GetType(FormBorderStyle))

End Sub

Private Sub ListBox1_SelectedIndexChanged( _
ByVal sender As Object, _
ByVal e As EventArgs _
) Handles ListBox1.SelectedIndexChanged
'Me.ListBox1.BackColor = _
' Color.FromKnownColor( _
' DirectCast(ListBox1.SelectedItem, KnownColor) _
' )
Me.FormBorderStyle = _
DirectCast(ListBox1.SelectedItem, FormBorderStyle)
End Sub

Either names or values are equally easy, its largely personally choice...

Hope this helps
Jay
 
C

Chris Dunaway

ComboBox1.Items.AddRange(MyEnum.GetNames(GetType(MyEnum)))

IMHO, it's better not to call shared methods using the variable name. i.e.
I think it is better to use this:

ComboBox1.Items.AddRange([Enum].GetNames(GetType(MyEnum)))

Note the use of [Enum] instead of MyEnum. To me this reinforces the idea
that the GetNames function is not an instance method but a shared one.

Just my 2c


--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top