exceptionz

Thoughts on Technology, Methodology and Programming.

Posts Tagged ‘code-construction’

Databind a Combobox to an Enum Property – Version 2.0

Posted by Marcus Wyatt on 18 April 2008

I’ve first posted this tip on the 14 of May 2005 and I still get good traffic to the post. So I decided to update the post and code samples to use Framework 2.0 (I’m still not using 3.5) and improve the code somewhat.

The problem:

How do I databind my combo box to my domain object that has a property of some enum type?

The solution:

Quite simple, firstly we will create an object (EnumComboBoxItem) that would encapsulate and represent each item in the combo list data source. EnumComboBoxItem is a pure fabrication class that has two properties we’ll use when databinding to the combo. We have a Value property which is used by the ValueMember property on the combo box and EscapedValue that is used by the DisplayMember property of the combo box. Note that when we construct a new EnumComboBoxItem instance that we call the AsciiEscape method in the constructor when populating the escapedValue field.

Here is the code:

1 using System;

2 using System.Collections.Generic;

3 using System.Text.RegularExpressions;

4

5 namespace BindComboToEnum

6 {

7 ///

8 /// The enum combo box item

9 ///

10 public class EnumComboBoxItem

11 {

12 #region Constants

13

14 public const string DisplayMember = “EscapedValue”;

15 public const string ValueMember = “Value”;

16

17 #endregion

18

19 #region Readonly & Static Fields

20

21 private readonly string escapedValue;

22 private readonly object value;

23

24 #endregion

25

26 #region C’tors

27

28 ///

29 /// Initializes a new instance of the class.

30 ///

31 ///

The original value.

32 public EnumComboBoxItem(object originalValue)

33 {

34 value = originalValue;

35 escapedValue = EscapeAsciiValues(originalValue.ToString());

36 }

37

38 #endregion

39

40 #region Instance Properties

41

42 ///

43 /// Gets the escaped value.

44 ///

45 /// The escaped value.

46 public string EscapedValue

47 {

48 get { return escapedValue; }

49 }

50

51 ///

52 /// Gets the value.

53 ///

54 /// The value.

55 public object Value

56 {

57 get { return value; }

58 }

59

60 #endregion

61

62 #region Instance Methods

63

64 ///

65 /// Returns a that represents the current .

66 ///

67 ///

68 /// A that represents the current .

69 ///

70 public override string ToString()

71 {

72 return escapedValue;

73 }

74

75 #endregion

76

77 #region Class Methods

78

79 ///

80 /// Creates the data source for the enum type.

81 ///

82 ///

Type of the enum.

83 /// When the type passed is not an enum the exception is raised.

84 ///

85 /// Returns a generic list to be used as the combo datasource.

86 ///

87 public static List<object> CreateDataSourceFor(Type enumType)

88 {

89 if (!enumType.IsEnum)

90 throw new ApplicationException(“When using the Enum combo data source you are only allowed to use enum types.”);

91

92 Array enumValues = Enum.GetValues(enumType);

93

94 List<object> result = new List<object>();

95

96 for (int index = 0; index < enumValues.Length; index++)

97 {

98 result.Add(new EnumComboBoxItem(enumValues.GetValue(index)));

99 }

100

101 return result;

102 }

103

104 private static string ConvertAsciiValueToStringRepresentation(string currentRegexMatchValue)

105 {

106 string result = string.Empty;

107 const int BaseValue = 16;

108

109 if (currentRegexMatchValue.StartsWith(“0x”))

110 {

111 // grab the remainder string after the 0x

112 string asciiCodeValue = currentRegexMatchValue.Substring(2);

113 // convert the remainder string to a byte

114 byte byteValue = Convert.ToByte(asciiCodeValue, BaseValue);

115 // now we can convert to a character and find the string of the character

116 result = Convert.ToChar(byteValue).ToString();

117 }

118 return result;

119 }

120

121 private static string EscapeAsciiValues(string rawValue)

122 {

123 string result = string.Empty;

124

125 if (rawValue == null)

126 return result;

127

128 // Find 0x with any four alpha-numeric characters

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

130

131 MatchCollection matchCollection = regex.Matches(rawValue);

132 Match matchPrevious = null;

133

134 for (int i = 0; i < matchCollection.Count; i++)

135 {

136 // if no match (ie. null) set to 0, else the current index + length

137 int startIndex = (matchPrevious == null ? 0 : matchPrevious.Index + matchPrevious.Length);

138

139 // add the part of the string that is not the ascii expression

140 result += rawValue.Substring(startIndex, matchCollection[i].IndexstartIndex);

141

142 // convert the ascii expression to a normal string

143 result += ConvertAsciiValueToStringRepresentation(matchCollection[i].Value);

144

145 matchPrevious = matchCollection[i];

146 }

147

148 if (matchPrevious == null)

149 result += rawValue;

150 else

151 result += rawValue.Substring(matchPrevious.Index + matchPrevious.Length);

152

153 return result;

154 }

155

156 #endregion

157 }

158 }

The EscapeAsciiValues 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:

1 namespace BindComboToEnum

2 {

3 public enum PaymentFrequencies

4 {

5 Annual = 1,

6 Bi0x00A00x002D0x00A0Annual = 2, // Bi – Annual

7 Quarterly = 4,

8 Monthly = 12,

9 Fortnightly = 26,

10 Weekly = 52

11 }

12 }

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

22 private void InitDataBindings()

23 {

24 PaymentFrequencyComboBox.DataSource = EnumComboBoxItem.CreateDataSourceFor(typeof(PaymentFrequencies));

25 PaymentFrequencyComboBox.DisplayMember = EnumComboBoxItem.DisplayMember;

26 PaymentFrequencyComboBox.ValueMember = EnumComboBoxItem.ValueMember;

27 PaymentFrequencyComboBox.DataBindings.Add(“SelectedValue”, new Loan(), “PaymentFrequency”);

28 }

The EnumComboBoxItem.CreateDataSourceFor method creates an List<object> for us to use as a data source for the combo box.

And there you have it, a Combobox bound to an Enum!

Advertisements

Posted in Software | Tagged: , , | Leave a Comment »