The framework already has a mechanism for you to do this...and it reduces the code by a lot!
For future reference, there is also a built-in function in StrataFrame to do this very thing. MicroFour.StrataFrame.Tools.Common.GetEnumDisplayValue(yourEnum).
Hi Dustin,
In this very same topic, today I just hit an Enum Display problem in a Dialog Browser in which the enum is not properly displayed when selected in field for the browser result set as in this picture:
I guess the SF combobox is using the MicroFour.StrataFrame.Tools.Common.GetEnumDisplayValue(EnumValue) but the field in the browser result is not. I fixed my code by changing my DB class and populate the CustomerType field throug the RowPopulating event which works nice, but I wonder if the framework should use the GetEnumDisplayValue() every where an enum is used.
Here is my BD code:
Public Class CustomerBrowserDialog Private Sub CustomerBrowserDialog_RowPopulating(ByVal e As MicroFour.StrataFrame.UI.Windows.Forms.RowPopulatingEventArgs) Handles MyBase.RowPopulating '-- Strong type the passed business object With CType(e.BusinessObject, atr_crm.Business.bizCustomers) '-- Format the customer type enum to display properly. e.Values(4).DisplayValue = MicroFour.StrataFrame.Tools.Common.GetEnumDisplayValue(.CustomerType) End With End SubEnd Class
Public
e.Values(4).DisplayValue = MicroFour.StrataFrame.Tools.Common.GetEnumDisplayValue(.CustomerType)
End
BTW, thanks to Greg and Leonard, this post came up just on time with my situation, even though I used Dustin approach .
This is pretty much what I did at first (only with Generic method and looping through fields and attributes using reflections) :
private static string GetDisplayValueType<T>(Enum customEnum) where T : struct {
string retValue = string.Empty; foreach (FieldInfo field in typeof(T).GetFields(BindingFlags.Static | BindingFlags.GetField | BindingFlags.Public)) {
object value = field.GetValue(null);
foreach (Attribute attrib in field.GetCustomAttributes(true)) {
if (attrib is EnumDisplayValueAttribute) {
EnumDisplayValueAttribute customAttr = (EnumDisplayValueAttribute)attrib; if (customEnum.Equals(value)) { retValue = customAttr.ValueOrKey; break; } }
}
return retValue;
Using your code, I was able to get rid of loops:
private static string GetDisplayValueType1<T>(Enum customEnum) where T : struct {
string desc = string.Empty; FieldInfo field = typeof(T).GetField(Enum.GetName(typeof(T), customEnum)); EnumDisplayValueAttribute descrAttr = (EnumDisplayValueAttribute)Attribute.GetCustomAttribute(field, typeof(EnumDisplayValueAttribute)); if (descrAttr != null) desc = descrAttr.ValueOrKey; else desc = customEnum.ToString();
return desc;
The second function looks much cleaner Thanks!
Lets seay I have this enum type:
public enum MyEnum { [EnumDisplayValue("This is Value 1")] value1 = 1, [EnumDisplayValue("This is Value 2")] value2 = 2 }
Now consider this code:
MyEnum myEnum = MyEnum.value2;MessageBox.Show(myEnum.ToString());
The messagebox displays "value2", but I would like to see EnumDisplayValue "This is Value 2".
Is there an easy way to get the value of that attribute? I have a feeling I need to use reflections to get it.
Thank you for your help.