<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://msmvps.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Deborah's Developer MindScape : WinForms, Extension Method</title><link>http://msmvps.com/blogs/deborahk/archive/tags/WinForms/Extension+Method/default.aspx</link><description>Tags: WinForms, Extension Method</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>Enum: Binding to the Description Attribute</title><link>http://msmvps.com/blogs/deborahk/archive/2009/07/10/enum-binding-to-the-description-attribute.aspx</link><pubDate>Fri, 10 Jul 2009 23:31:04 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1699394</guid><dc:creator>Deborah Kurata</dc:creator><slash:comments>12</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/deborahk/rsscomments.aspx?PostID=1699394</wfw:commentRss><comments>http://msmvps.com/blogs/deborahk/archive/2009/07/10/enum-binding-to-the-description-attribute.aspx#comments</comments><description>&lt;p&gt;The Enum keyword allows you to define a standard set of named constants for use in your application. Sometimes you may want to present this same list of&amp;#160; values to your user. &lt;/p&gt;  &lt;p&gt;You can display the set of Enum values in a ComboBox or ListBox using data binding. And even better, if you use the DescriptionAttribute in the Enum, you can display a more user-friendly set of values.&lt;/p&gt;  &lt;p&gt;For example, a Customer business object may have a CustomerType defined with a specific set of options for corporations, individuals, schools, and charities. You want to use these values in your code, but also display them to the user in a ComboBox.&lt;/p&gt;  &lt;h2&gt;Enum Definition&lt;/h2&gt;  &lt;p&gt;The Enum can reside in the Customer class, or in its own class.&lt;/p&gt;  &lt;p&gt;In C#:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;public enum CustomerTypeOption      &lt;br /&gt;{       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; NotDefined,&amp;#160; &lt;br /&gt;&lt;/font&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; Corp,&amp;#160; &lt;br /&gt;&lt;/font&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; IndividualorFamily,&amp;#160; &lt;br /&gt;&lt;/font&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; SchoolorUniversity,&amp;#160; &lt;br /&gt;&lt;/font&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; Charity      &lt;br /&gt;}&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;In VB:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Public Enum CustomerTypeOption      &lt;br /&gt;&amp;#160;&amp;#160; NotDefined       &lt;br /&gt;&amp;#160;&amp;#160; Corp       &lt;br /&gt;&amp;#160;&amp;#160; IndividualorFamily       &lt;br /&gt;&amp;#160;&amp;#160; SchoolorUniversity       &lt;br /&gt;&amp;#160;&amp;#160; Charity       &lt;br /&gt;End Enum&lt;/font&gt;&lt;/p&gt;  &lt;h2&gt;CustomerType Property&lt;/h2&gt;  &lt;p&gt;A CustomerType property in the Customer class can use this enum type.&lt;/p&gt;  &lt;p&gt;In C#:&lt;/p&gt;  &lt;p&gt;&lt;font face="Consolas"&gt;public CustomerTypeOption CustomerType { get; set; }&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;In VB:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Private _CustomerType As CustomerTypeOption      &lt;br /&gt;Public Property CustomerType() As CustomerTypeOption       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Get       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Return _CustomerType       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End Get       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Set(ByVal value As CustomerTypeOption)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; _CustomerType = value       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End Set       &lt;br /&gt;End Property&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;The C# code uses the automatically implemented properties feature introduced with .NET 3.5. The VB code uses the full property definition.&lt;/p&gt;  &lt;h2&gt;Binding To an Enum&lt;/h2&gt;  &lt;p&gt;The Enum can also be used by the user interface. For example, you can bind a comboBox or listBox to a set of customer type options.&lt;/p&gt;  &lt;p&gt;In C#:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Dictionary&amp;lt;string, int&amp;gt; CustomerTypeList =      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new Dictionary&amp;lt;string, int&amp;gt;();       &lt;br /&gt;      &lt;br /&gt;foreach (int enumValue in       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Enum.GetValues(typeof(Customer.CustomerTypeOption)))       &lt;br /&gt;{       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; CustomerTypeList.Add(       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="#65402e" face="Consolas"&gt;Enum.GetName(typeof(Customer.CustomerTypeOption),      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; enumValue ), enumValue);       &lt;br /&gt;} &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;// Bind the customer type combo box      &lt;br /&gt;CustomerTypeComboBox.DisplayMember = &amp;quot;Key&amp;quot;;       &lt;br /&gt;CustomerTypeComboBox.ValueMember = &amp;quot;Value&amp;quot;;       &lt;br /&gt;CustomerTypeComboBox.DataSource = new BindingSource(CustomerTypeList,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; null);&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;In VB:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Dim CustomerTypeList As New Dictionary(Of String, Integer)      &lt;br /&gt;      &lt;br /&gt;For Each enumValue As Integer In _       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; [Enum].GetValues(GetType(Customer.CustomerTypeOption))       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; CustomerTypeList.Add( _       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; [Enum].GetName(GetType(Customer.CustomerTypeOption), _       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; enumValue), enumValue)       &lt;br /&gt;Next &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#39; Bind the combo box      &lt;br /&gt;CustomerTypeComboBox.DisplayMember = &amp;quot;Key&amp;quot;       &lt;br /&gt;CustomerTypeComboBox.ValueMember = &amp;quot;Value&amp;quot;       &lt;br /&gt;CustomerTypeComboBox.DataSource = New BindingSource(CustomerTypeList, _       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Nothing)&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;This code defines a Dictionary to contain the name of the enum and the integer value of the enum. The for/each loop iterates through the set of Enum values and adds the name and value to the dictionary.&lt;/p&gt;  &lt;p&gt;The code then binds to the ComboBox, setting the DisplayMember to the “Key” of the dictionary and the ValueMember to the “Value” of the Dictionary.&lt;/p&gt;  &lt;p&gt;The result looks like this:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/deborahk.metablogapi/7853.image_5F00_2296F6DF.png"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="image" border="0" alt="image" src="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/deborahk.metablogapi/0511.image_5F00_thumb_5F00_6BBCA298.png" width="236" height="96" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;While it does provide the appropriate options, it is not very user-friendly. It would be nicer if there were appropriate spaces and full words. For that, you can use the DescriptionAttribute on the Enum.&lt;/p&gt;  &lt;h2&gt;Using the DescriptionAttribute&lt;/h2&gt;  &lt;p&gt;NOTE: Be sure to import the &lt;strong&gt;System.ComponentModel&lt;/strong&gt; and the &lt;strong&gt;System.Reflection&lt;/strong&gt; namespaces.&lt;/p&gt;  &lt;p&gt;In C#:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;public enum CustomerTypeOption      &lt;br /&gt;{       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; [DescriptionAttribute(&amp;quot;Not Specified&amp;quot;)]       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; NotDefined, &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; [DescriptionAttribute(&amp;quot;Corporation&amp;quot;)]      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Corp, &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; [DescriptionAttribute(&amp;quot;Individual or Family&amp;quot;)]      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; IndividualorFamily, &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; [DescriptionAttribute(&amp;quot;School or University&amp;quot;)]      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; SchoolorUniversity, &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; [DescriptionAttribute(&amp;quot;Charity&amp;quot;)]      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Charity       &lt;br /&gt;}&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;In VB:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Public Enum CustomerType      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;DescriptionAttribute(&amp;quot;Corporate Customer&amp;quot;)&amp;gt; _       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; CorpCustomer &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;DescriptionAttribute(&amp;quot;Individual or Family&amp;quot;)&amp;gt; _      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; IndividualorFamily &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;DescriptionAttribute(&amp;quot;School or University&amp;quot;)&amp;gt; _      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; SchoolorUniversity &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;DescriptionAttribute(&amp;quot;Charity&amp;quot;)&amp;gt; _      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Charity &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;DescriptionAttribute(&amp;quot;Kingdom&amp;quot;)&amp;gt; _      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Kingdom       &lt;br /&gt;End Enum&lt;/font&gt; &lt;/p&gt;  &lt;h2&gt;Binding to an Enum Description&lt;/h2&gt;  &lt;p&gt;Now you can bind to the description instead of to the Enum constant.&lt;/p&gt;  &lt;p&gt;In C#:&lt;/p&gt;  &lt;p&gt;&lt;font face="Consolas"&gt;Dictionary&amp;lt;string, int&amp;gt; CustomerTypeList =      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new Dictionary&amp;lt;string, int&amp;gt;(); &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Consolas"&gt;FieldInfo fi;      &lt;br /&gt;DescriptionAttribute da;       &lt;br /&gt;foreach (Customer.CustomerTypeOption enumValue in       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Enum.GetValues(typeof(Customer.CustomerTypeOption)))       &lt;br /&gt;{       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; fi = typeof(Customer.CustomerTypeOption).       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; GetField((enumValue.ToString()));       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; da = (DescriptionAttribute)Attribute.GetCustomAttribute(fi,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; typeof(DescriptionAttribute));       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; if (da != null)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; CustomerTypeList.Add(da.Description, (int)enumValue);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;} &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Consolas"&gt;// Bind the customer type combo box      &lt;br /&gt;CustomerTypeComboBox.DisplayMember = &amp;quot;Key&amp;quot;;       &lt;br /&gt;CustomerTypeComboBox.ValueMember = &amp;quot;Value&amp;quot;;       &lt;br /&gt;CustomerTypeComboBox.DataSource = new BindingSource(CustomerTypeList,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; null);&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;In VB:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Dim CustomerTypeList As New Dictionary(Of String, Integer)      &lt;br /&gt;      &lt;br /&gt;Dim fi As FieldInfo       &lt;br /&gt;Dim da As DescriptionAttribute       &lt;br /&gt;For Each enumValue As Customer.CustomerTypeOption In _       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; [Enum].GetValues(GetType(Customer.CustomerTypeOption))       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; fi = GetType(Customer.CustomerTypeOption). _       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; GetField(enumValue.ToString)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; da = DirectCast(Attribute.GetCustomAttribute(fi, _       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; GetType(DescriptionAttribute)), DescriptionAttribute)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; If da IsNot Nothing Then       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; CustomerTypeList.Add(da.Description, enumValue)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End If       &lt;br /&gt;Next &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#39; Bind the combo box      &lt;br /&gt;CustomerTypeComboBox.DisplayMember = &amp;quot;Key&amp;quot;       &lt;br /&gt;CustomerTypeComboBox.ValueMember = &amp;quot;Value&amp;quot;       &lt;br /&gt;CustomerTypeComboBox.DataSource = New BindingSource(CustomerTypeList, _       &lt;br /&gt;&lt;/font&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Nothing) &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;This code defines a Dictionary to contain the description of the enum and the integer value of the enum. The for/each loop iterates through the set of Enum values. It uses reflection to find the DescriptionAttribute. It then adds the description and value to the dictionary. &lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="ver"&gt;The code then binds to the ComboBox, setting the DisplayMember to the “Key” of the dictionary and the ValueMember to the “Value” of the Dictionary.&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="ver"&gt;&lt;font color="#65402e"&gt;Now the combo&lt;/font&gt; box looks like this:&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/deborahk.metablogapi/3568.image_5F00_2B1A5629.png"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="image" border="0" alt="image" src="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/deborahk.metablogapi/7360.image_5F00_thumb_5F00_1F8498EA.png" width="234" height="94" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Much better!&lt;/p&gt;  &lt;h2&gt;Extension Methods&lt;/h2&gt;  &lt;p&gt;So the user interface looks nicer, but the code to get the DescriptionAttribute looks rather tedious. It would be much nicer if there was a GetDescription method on the Enum. But hey, with the extension method feature introduced in .NET 3.5, we can build one ourselves!&lt;/p&gt;  &lt;p&gt;In C#:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;public static string GetDescription(this Enum currentEnum)      &lt;br /&gt;{       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; string description = String.Empty;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; DescriptionAttribute da ; &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; FieldInfo fi = currentEnum.GetType().      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; GetField(currentEnum.ToString());       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; da = (DescriptionAttribute)Attribute.GetCustomAttribute(fi,&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="#65402e" face="Consolas"&gt;typeof(DescriptionAttribute));      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; if (da != null)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; description = da.Description;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; else       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; description = currentEnum.ToString(); &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; return description;      &lt;br /&gt;&lt;/font&gt;&lt;font color="#65402e" face="Consolas"&gt;}&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;In VB:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;lt;ExtensionAttribute()&amp;gt; _      &lt;br /&gt;Public Function GetDescription(ByVal currentEnum As [Enum]) As String       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Dim description As String = String.Empty       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Dim da As DescriptionAttribute &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; Dim fi As FieldInfo = currentEnum.GetType. _      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; GetField(currentEnum.ToString)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; da = DirectCast(Attribute.GetCustomAttribute(fi, _       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; GetType(DescriptionAttribute)), DescriptionAttribute)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; If da IsNot Nothing Then       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; description = da.Description       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Else       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; description = currentEnum.ToString       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End If &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; Return description      &lt;br /&gt;End Function&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;The C# code can reside in any static class. The VB code must reside in a module, which provides the same features as a static class.&lt;/p&gt;  &lt;p&gt;This code uses reflection to get the DescriptionAttribute for any Enum value. So it can be readily reused. The code returns the DescriptionAttribute if one is found, otherwise it returns the name.&lt;/p&gt;  &lt;h2&gt;Binding to an Enum Description - Redux&lt;/h2&gt;  &lt;p&gt;Now the binding code is much easier to work with.&lt;/p&gt;  &lt;p&gt;In C#:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Dictionary&amp;lt;string, int&amp;gt; CustomerTypeList =      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new Dictionary&amp;lt;string, int&amp;gt;(); &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;foreach (Customer.CustomerTypeOption enumValue in      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Enum.GetValues(typeof(Customer.CustomerTypeOption)))       &lt;br /&gt;{       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; CustomerTypeList.Add(enumValue.GetDescription(), (int)enumValue);       &lt;br /&gt;} &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;// Bind the customer type combo box      &lt;br /&gt;CustomerTypeComboBox.DisplayMember = &amp;quot;Key&amp;quot;;       &lt;br /&gt;CustomerTypeComboBox.ValueMember = &amp;quot;Value&amp;quot;;       &lt;br /&gt;CustomerTypeComboBox.DataSource = new BindingSource(CustomerTypeList,&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; null);&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;In VB:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Dim CustomerTypeList As New Dictionary(Of String, Integer)      &lt;br /&gt;      &lt;br /&gt;For Each enumValue As Customer.CustomerTypeOption In _       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; [Enum].GetValues(GetType(Customer.CustomerTypeOption))       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; CustomerTypeList.Add(enumValue.GetDescription, enumValue)       &lt;br /&gt;Next &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#39; Bind the combo box      &lt;br /&gt;CustomerTypeComboBox.DisplayMember = &amp;quot;Key&amp;quot;       &lt;br /&gt;CustomerTypeComboBox.ValueMember = &amp;quot;Value&amp;quot;       &lt;br /&gt;CustomerTypeComboBox.DataSource = New BindingSource(CustomerTypeList, _       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Nothing)&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;Enjoy!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1699394" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/deborahk/archive/tags/VB/default.aspx">VB</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/.NET/default.aspx">.NET</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/CSharp/default.aspx">CSharp</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/Enum/default.aspx">Enum</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/Binding/default.aspx">Binding</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/Extension+Method/default.aspx">Extension Method</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/WinForms/default.aspx">WinForms</category></item></channel></rss>