Friendly member names for Enumerations

T

Turbot

Hello,

I have an ENUM that looks like this:

Public Enum Locations As Byte
Top_Left
Top_Middle
Top_Right
Middle_Left
Middle_Middle
Middle_Right
Bottom_Left
Bottom_Middle
Bottom_Right
End Enum

I am then using the following code to automatically list the enum
members/values in a combo box:

cmbBGStyle.DataSource = [Enum].GetValues(GetType(Locations))

Here is my quandry - As you can see, I have had to place underscores in
the enum member names as I can't have spaces. I was wondering if anyone
knows of an attribute or something that will enable me to display a
friendlier name in the combo box such as 'Top Left', 'Bottom Right'
etc.
 
C

Cor Ligthert

Turbot,

Any reason you do not what you want in the standard way?

\\\\
Private Sub Form14_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim myArray As New ArrayList
myArray.Add(New myzones("Top Left", 1))
myArray.Add(New myzones("Top Middle", 2))
myArray.Add(New myzones("Top Rigth", 3))
ComboBox1.DataSource = myArray
ComboBox1.DisplayMember = "Description"
ComboBox1.ValueMember = "Number"
End Sub
////
Public Class myzones
Private mName As String
Private mNumber As Integer
Public Sub New(ByVal Descript As String, _
ByVal numb As Integer)
mName = Descript
mNumber = numb
End Sub
Public ReadOnly Property Description() As String
Get
Return mName
End Get
End Property
Public ReadOnly Property Number() As Integer
Get
Return mNumber
End Get
End Property
End Class
///

I hope this sample I made for you helps

Cor
 
I

Imran Koradia

Here's how you could go about this:

Imports System.Reflection
Imports System.ComponentModel

Public Enum LocationEnum
<Description("Top")> Top = 1
<Description("Bottom")> Bottom = 2
<Description("Left")> Left = 3
<Description("Right")> Right = 4
<Description("Top Left")> TopLeft = 5
<Description("Top Right")> TopRight = 6
<Description("Bottom Left")> BottomLeft = 7
<Description("Bottom Right")> BottomRight = 8
End Enum

Private Sub LoadListBox()
For Each en As [Enum] In _
[Enum].GetValues(GetType(LocationEnum))
Dim fi As FieldInfo = _
en.GetType().GetField(en.ToString)
Dim attr() As DescriptionAttribute = _
DirectCast(fi.GetCustomAttributes( _
GetType(DescriptionAttribute), False), _
DescriptionAttribute())
ListBox1.Items.Add(attr(0).Description)
Next
End Sub

The idea is to use the description attribute under the System.ComponentModel
namespace.

hope that helps..
Imran.
 
H

Herfried K. Wagner [MVP]

Turbot said:
Public Enum Locations As Byte
Top_Left
Top_Middle
Top_Right
Middle_Left
Middle_Middle
Middle_Right
Bottom_Left
Bottom_Middle
Bottom_Right
End Enum

I am then using the following code to automatically list the enum
members/values in a combo box:

cmbBGStyle.DataSource = [Enum].GetValues(GetType(Locations))

Here is my quandry - As you can see, I have had to place underscores in
the enum member names as I can't have spaces. I was wondering if anyone
knows of an attribute or something that will enable me to display a
friendlier name in the combo box such as 'Top Left', 'Bottom Right'

Adding descriptions to enumeration constants
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=enumdescription&lang=en>

Creating "enumerations" of any data type
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=anytypeenums&lang=en>
 

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

Similar Threads


Top