Bind a combobox to an enum property





0
Date Submitted Wed. Jan. 30th, 2008 9:24 PM
Revision 1 of 1
Helper wallie
Tags combobox | enum
Comments 0 comments
binding a combobox to an enum property


using System.Text.RegularExpressions;


public class ComboBoxEnumItem

{

   private object value;

   private string toString;

   private string displayItem;

 

   public object Value

   {

      get

      {

         return value;

      }

   }

 

   public override string ToString()

   {

      return toString;

   }

 

   public string DisplayItem

   {

      get

      {

         return displayItem;

      }

   }

 

   public ComboBoxEnumItem( object value )

   {

      this.value = value;

      displayItem = AsciiEscape( value.ToString() );

      toString = displayItem;

   }

 

   private string AsciiEscape( string value )

   {

      if( value == null )

         return null;

     

      string returnValue = “”;

      Regex r = new Regex(“0x[0-9A-Z]{4});

      MatchCollection m = r.Matches( value );

      Match mtchPrevious = null;

     

      for( int i = 0; i < m.count; i++ )

      {

         int istartindex = (mtchprevious == null ? 0: mtchprevious.index + mtchprevious.length );

         returnvalue += value.substring( istartindex, m[i].index - istartindex );

         string strgroup = m[ i ].value;

         

         if( strgroup.startswith(“0x”) )

         {

            string strcode = strgroup.substring(2);

            byte bval = system.convert.tobyte( strcode, 16 );

            string s = new string((char)bval,1);

            returnvalue += s;

         }

         

         mtchprevious = m[i];

      }

     

      if ( mtchprevious == null )

      {

         returnvalue += value;

      }

      else

      {

         returnvalue += value.substring( mtchprevious.index + mtchprevious.length );

      }

     

      return returnvalue;

   }

}

//The AsciiEscape method is used to Replace the Ascii characters in our //Enum with the character representation. We do the Ascii characters //because we want our combo to display “Bi- Annual” instead of //“BiAnnual”. Look at the following enum as an example:

public enum PaymentFrequencies

{

   Annual = 1,

   Bi0x00A00x002D0x00A0Annual = 2,

   Quarterly = 4, Monthly = 12,

   Fortnightly = 26,

   Weekly = 52

}

//Now, all that we need is the following code behind the form to bind our Combobox and we //are away:

private void InitDataBindings()

{

   cboEntity.DataSource = GetEnumComboDataSource( PaymentFrequencies );

   cboEntity.DisplayMember = “DisplayItem”;

   cboEntity.ValueMember = “Value”;

   cboEntity.DataBindings.Add( “SelectedValue”, LoanLogic, “PaymentFrequency” );

}

 

private ArrayList GetEnumComboDataSource( Type type )

{

   ArrayList comboData = new ArrayList();

   Array ary = Enum.GetValues( type );

   

   for( int j = 0; j < ary.length; j++ )

   {

      comboData.Add( new ComboBoxEnumItem( ary.GetValue( j ) ) );

   }

   

   return comboData;

}

// The GetEnumComboDataSource method creates an ArrayList for us to use as a data source for the combobox.

 

walter chao

Comments

There are currently no comments for this snippet.

Voting

Votes Up


Votes Down