Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » C# » General » Downloads
 
Add your own
alternative version

Mapping Text to Enum entries

, 6 Jun 2003
Attaching a description to each entry in an enum.
enumwithdescription_demo.zip
App.ico
EnumWithDescription.csproj.user
enumwithdescription_src.zip
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Reflection;

namespace EnumWithDescription
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class FormTest : System.Windows.Forms.Form
	{
		private System.Windows.Forms.ListView listView;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public FormTest()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.listView = new System.Windows.Forms.ListView();
			this.SuspendLayout();
			// 
			// listView
			// 
			this.listView.Dock = System.Windows.Forms.DockStyle.Fill;
			this.listView.Font = new System.Drawing.Font("Courier New", 8.139131F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.listView.Name = "listView";
			this.listView.Size = new System.Drawing.Size(464, 138);
			this.listView.TabIndex = 0;
			// 
			// FormTest
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
			this.ClientSize = new System.Drawing.Size(464, 138);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.listView});
			this.Name = "FormTest";
			this.Text = "Enum with Description";
			this.Load += new System.EventHandler(this.FormTest_Load);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new FormTest());
		}


		private enum MyColors
		{
			[Description("yuk!")]       LightGreen    = 0x012020,
			[Description("nice :-)")]   VeryDeepPink  = 0x123456,
			[Description("so what")]    InvisibleGray = 0x456730,
			[Description("no comment")] DeepestRed    = 0xfafafa,
			[Description("I give up")]  PitchBlack    = 0xffffff,
		}

		private void FormTest_Load(object sender, System.EventArgs e)
		{
			listView.View=View.Details;

			listView.Columns.Add("Name"       , listView.ClientSize.Width/4, HorizontalAlignment.Left);
			listView.Columns.Add("Value"      , listView.ClientSize.Width/4, HorizontalAlignment.Right);
			listView.Columns.Add("Description", listView.ClientSize.Width/2, HorizontalAlignment.Left);

			foreach (MyColors color in Enum.GetValues(typeof(MyColors))) 
			{
				ListViewItem item = new ListViewItem(color.ToString());
				item.SubItems.Add(string.Format("0x{0:x6}", (int)color));
				item.SubItems.Add(GetDescription(color));
				listView.Items.Add(item);
			}

		}

		public static string GetDescription(Enum value)
		{
			FieldInfo fi=value.GetType().GetField(value.ToString());
			DescriptionAttribute[] attributes = (DescriptionAttribute[]) fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
			return (attributes.Length>0)?attributes[0].Description:value.ToString();
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Reto Ravasio

Switzerland Switzerland
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141223.1 | Last Updated 7 Jun 2003
Article Copyright 2003 by Reto Ravasio
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid