26282 total geeks with 3498 solutions
Recent challengers:
 Welcome, you are an anonymous user! [register] [login] Get a yourname@osix.net email address 

Articles

GEEK

User's box
Username:
Password:

Forgot password?
New account

Shoutbox
MaxMouse
It's Friday... That's good enough for me!
CodeX
non stop lolz here but thats soon to end thanks to uni, surely the rest of the world is going good?
stabat
how things are going guys? Here... boring...
CodeX
I must be going wrong on the password lengths then, as long as it was done on ECB
MaxMouse
lol... the key is in hex (MD5: of the string "doit" without the "'s) and is in lower case. Maybe i should have submitted this as a challenge!

Donate
Donate and help us fund new challenges
Donate!
Due Date: May 31
May Goal: $40.00
Gross: $0.00
Net Balance: $0.00
Left to go: $40.00
Contributors


News Feeds
The Register
"Nuke-busting"
Stuxnet HELPED
Iranian nuke
project, says
boffin
MYSTERY Nokia Lumia
with
gazillion-pixel
camera "spotted"
Machine learning
climbs atop Hadoop
New 4TB drive
spaffs half a telly
season into your
eyes AT ONCE
Buff American
beauties keen to
dominate Euro youth
in tech tussle
Indian "attacks"
Norwegian telco to
get at Pakistan,
China
Robots roll to make
GPS
millimetre-perfect
Dell"s
PC-on-a-stick
landing in July:
report
Global perils of
dirt, glaciers and
lizardocalypse
overblown, say
boffins
Infosys vows to
fight Indian tax
claim
Slashdot
The Hunt For
LulzSec"s Missing
Sixth Member
Latvian Police Raid
Teacher"s Home for
Uploading $4.00
Textbook
EFF Resumes
Accepting Bitcoin
Donations After Two
Year Hiatus
Google Drops XMPP
Support
Motion To Delay
Sanctions Against
Prenda Lawyers
Denied
NSA Data Center the
Focus of Tax
Controversy
Viruses In Mucus
Protect From
Infection
Reporters
Threatened, Labeled
Hackers For Finding
Security Hole
Judges Debate
Patents and If New
Software Makes a
Computer a "New
Machine"
Steve Jackson Shows
Off the Texas Brick
Railroad (Video)
Article viewer

Using an enumeration as a data source for binding



Written by:gimlock
Published by:bb
Published on:2005-07-22 15:19:34
Topic:Dot.Net
Search OSI about Dot.Net.More articles by gimlock.
 viewed 16975 times send this article printer friendly

Digg this!
    Rate this article :
Sometimes you may have an enumeration of values that would be quite usefully used as data for a combo box or list box.

The following is a helper class which you can use to pass a combo box or list box and an enum for data binding.

using System;
using System.Collections;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace EnumTools
{
    public enum TRANSPORT : uint
    {
        Car = 1,
        Bus = 2,
        HandsomeCab = 3,
        Train = 4,
        Aeroplane = 5
    }

    public class BindableObject
    {
        private string m_strValue = "";
        private string m_strText = "";

        public BindableObject(string strValue, string strText)
        {
            m_strValue = strValue;
            m_strText = strText;
        }

        public string Value
        {
            get
            {
                return m_strValue;
            }
        }

        public string Text
        {
            get
            {
                return m_strText;
            }
        }
    }

    public class EnumBinder
    {
        public static void DataBind(ListControl objListControl, Type objEnumType)
        {
            Regex objRegEx = new Regex(@"[A-Z][a-z]*", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace);
            //
            // get the values from the enumeration
            //
            Array objValues = Enum.GetValues(objEnumType);
            ArrayList objBindObjects = new ArrayList();
            MatchCollection objMatches = null;
            string strValueName = "", strDisplayName = "";
            //
            // loop through each value
            //
            foreach(uint nValue in objValues)
            {
                strDisplayName = "";
                //
                // get the 'nice' name for the value
                //
                strValueName = Enum.GetName(objEnumType, nValue);
                //
                // split the nice name on camel casing
                //
                objMatches = objRegEx.Matches(strValueName);
                //
                // build up the display string
                //
                foreach(Match objMatch in objMatches)
                {
                    strDisplayName += objMatch.Value + " ";
                }
                //
                // add the current enum details as a bindable object
                //
                objBindObjects.Add(new BindableObject(nValue.ToString(), strDisplayName.Trim()));
            }
            //
            // bind up
            //
            objListControl.DataSource = null;
            objListControl.DisplayMember = "Text";
            objListControl.ValueMember = "Value";
            objListControl.DataSource = objBindObjects;
        }
    }
}


The TRANSPORT enum is only included in the above class for this example.

To then use this class to bind the example TRANSPORT enum to a combo box, do the following:

EnumTools.EnumBinder.DataBind(comboBox1, typeof(EnumTools.TRANSPORT));


You may pass a list box in place of the combo box variable above if that is your UI control of choice.

Did you like this article? There are hundreds more.

Comments:
Anonymous
2007-07-06 16:39:02
Very nice. Simple and elegant. Thanks for posting it.
dbirdz
2010-03-09 15:26:23
Very nice. However it did not present enough flexibility for my purposes so I went ahead and created a modified version which is more complete:
http://www.osix.net/modules/article/index.php?id=932
thnx for the inspiration! I would have abandoned if I was all on my own.
Anonymously add a comment: (or register here)
(registration is really fast and we send you no spam)
BB Code is enabled.
Captcha Number:


Blogs: (People who have posted blogs on this subject..)
bb
ASP.NET RadioButton GroupName when inside a Repeater on Sun 10th Jun 8am
I was thankful on finding this nugget of code, which makes the groupname work out when slamming in radiobuttons in an asp.net repeater. http://www.codeguru.com/csharp/csharp/cs _controls/custom/article.php/c12371/


     
Your Ad Here
 
Copyright Open Source Institute, 2006