Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7757385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:04:51+00:00 2026-06-01T13:04:51+00:00

i have a enum like that : public enum Mode { [*I need a

  • 0

i have a enum like that :

public enum Mode
{
[*"I need a display name attribute to show in combobox"*]
Active,
[*"I need a display name attribute to show in combobox"*]
DeActive
}

I want to use it as a dataSource in a comboBox and i need to set display name.
Anyone can help me?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T13:04:52+00:00Added an answer on June 1, 2026 at 1:04 pm

    it is not the BLT deal…

    my solution was:

    Test.cs

    using System;
    using DK.Common.Caption;
    using NUnit.Framework;
    
    namespace DK.Common.Tests
    {
        [TestFixture]
        public class LocalizedEnumConverterTests
        {
            const string FirstName = "Первый";
            const string FirstResName = "TestEnumFirst";
            const string SecondName = "Второй";
            const string SecondResName = "SecondResName";
            const string ThirdName = "Третий";
            const string FourthName = "Fourth";
            const string EmptyName = "This is empty!";
    
            [Flags]
            enum TestEnum
            {
                [Caption(FirstName)] 
                First = 1,
    
                [Caption(SecondName, SecondResName)] 
                Second = 2,
    
                [Caption(ThirdName)] 
                Third = 4,
    
                Fourth = 8,
    
                [Caption] 
                Empty = 16,
            }
    
            readonly LocalizedEnumConverter _converter = new LocalizedEnumConverter(typeof (TestEnum));
    
            [Test]
            public void ToStringTest()
            {
                Func<TestEnum, string> c = (e) => _converter.ConvertToString(null, e);
    
                Assert.AreEqual(FirstResName, c(TestEnum.First));
                Assert.AreEqual(SecondResName, c(TestEnum.Second));
                Assert.AreEqual(ThirdName, c(TestEnum.Third));
                Assert.AreEqual(EmptyName, c(TestEnum.Empty));
    
                Assert.AreEqual(SecondResName + ", " + FourthName, c(TestEnum.Second | TestEnum.Fourth));
            }
    
            [Test]
            public void FromStringTest()
            {
                Func<string, TestEnum> c = (s) => (TestEnum) _converter.ConvertFromString(null, s);
    
                Assert.AreEqual(TestEnum.First, c(FirstResName));
                Assert.AreEqual(TestEnum.Second, c(SecondResName));
                Assert.AreEqual(TestEnum.Third, c(ThirdName));
                Assert.AreEqual(TestEnum.Empty, c(EmptyName));
    
                Assert.AreEqual(TestEnum.Second | TestEnum.Fourth, c(SecondResName + ", " + FourthName));
            }
    
            [Test]
            public void FromToTest()
            {
                const TestEnum @enum = TestEnum.First | TestEnum.Second | TestEnum.Third | TestEnum.Fourth | TestEnum.Empty;
    
                var str = LocalizedEnumConverter.EnumToString(@enum);
                var en = LocalizedEnumConverter.StringToEnum<TestEnum>(str);
    
                Assert.AreEqual(@enum, en);
            }
        }
    }
    

    Caption.cs

    using System;
    
    
    namespace DK.Common.Caption
    {
        public class CaptionAttribute : Attribute
        {
            readonly string _caption;
    
            public CaptionAttribute()
                :this(string.Empty)
            {
            }
    
            public CaptionAttribute(string caption)
                :this(caption, string.Empty)
            {
            }
    
            public CaptionAttribute(string caption, string resourceName)
            {
                _caption = caption;
                ResourceName = resourceName;
            }
            public string Caption { get { return _caption; } }
    
            public string ResourceName { get; private set; }
        }
    }
    

    LocalizedEnumConverter.cs

    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Resources;
    using System.Text;
    using System.ComponentModel;
    using System.Reflection;
    using System.Threading;
    
    namespace DK.Common.Caption
    {
        using Names = Dictionary<object, string>;
        using Cultures = Dictionary<CultureInfo, Dictionary<object, string>>;
    
        public class LocalizedEnumConverter : EnumConverter
        {
    private int Dummy(int myI)
    {
       int i = 10;
       int j = 5;
       return myI+i*j;
    }       public LocalizedEnumConverter(Type type) : base(type)
            {
            }
    
            public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
            {
                Type type = this.EnumType;
                bool isFlags = type.GetCustomAttributes(typeof(FlagsAttribute), false).Length > 0;
    
                if (destinationType == typeof(string))
                {
                    string s = "";
                    foreach (object val in Enum.GetValues(type))
                    {
                        if ((((int)value & (int)val) != 0 && isFlags)
                            || ((int)value == 0 && (int)val == 0)
                            || Enum.Equals(value, val))
                        {
                            CaptionAttribute[] atr = (CaptionAttribute[])(type.GetField(Enum.GetName(type, val))).GetCustomAttributes(typeof(CaptionAttribute), true);
                            if (atr.Length > 0)
                                s += ", " + GetNames(EnumType, culture)[val];
                            else
                                s += ", " + base.ConvertTo(context, culture, val, destinationType);
                        }
                    }
                    if (s != "") return s.Substring(2);
                }
                return base.ConvertTo(context, culture, value, destinationType);
            }
    
            public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
            {
                if (value.GetType() == typeof(string))
                {
                    string       caption  = (string)value;
                    List<string> splitted = new List<string>(caption.Split(new string[] { ", " }, StringSplitOptions.RemoveEmptyEntries));
                    //FieldInfo[]  fields   = EnumType.GetFields();
                    string       mapped   = "";
    
                    //foreach (FieldInfo fi in fields)
                    //{
                    //    CaptionAttribute[] atr = (CaptionAttribute[])fi.GetCustomAttributes(typeof(CaptionAttribute), true);
    
                    //    if (atr.Length > 0 && splitted.IndexOf(GetLocalizedCaption(atr[0], value, culture)) >= 0)
                    //        mapped += "," + fi.Name;
                    //}
    
                    var names = GetNames(EnumType, culture);
    
                    foreach (var val in splitted)
                    {
                        foreach (var name in names)
                        {
                            if(name.Value == val)
                            {
                                mapped += "," + name.Key.ToString();
    
                                break;
                            }
                        }
                    }
    
                    if (mapped.Length > 0)
                    {
                        object o = base.ConvertFrom(context, culture, mapped.Substring(1)/*fi.Name*/);
                        return o;
                    }
    
                }
                return base.ConvertFrom(context, culture, value);
            }
    
            private static string GetLocalizedCaption(Type type, CaptionAttribute attr, object val, System.Globalization.CultureInfo culture)
            {
                var resName = attr.ResourceName;
    
                if (string.IsNullOrEmpty(resName))
                {
                    resName = type.Name + val.ToString();
                }
    
                var m = GetResourceManager(type);
                try
                {
                    var caption = m != null ? m.GetString(resName, culture) : string.Empty;
    
                    return string.IsNullOrEmpty(caption) ? attr.Caption : caption;
                }
                catch
                {
                    return attr.Caption;
                }
            }
    
            private static readonly Dictionary<Assembly, ResourceManager> _managers = new Dictionary<Assembly, ResourceManager>();
            private static  readonly Dictionary<Type, Cultures> _names = new Dictionary<Type, Cultures>();
    
            private static Names GetNames(Type type, CultureInfo cultureInfo)
            {
                lock (_names)
                {
                    Cultures cult;
                    if(!_names.TryGetValue(type, out cult))
                    {
                        _names[type] = new Cultures();
                        cult = _names[type];
                    }
    
                    Names n;
    
                    if (cultureInfo == null)
                        cultureInfo = Thread.CurrentThread.CurrentCulture;
    
                    if(!cult.TryGetValue(cultureInfo, out n))
                    {
                        n = new Names();
                        foreach (object val in Enum.GetValues(type))
                        {
                            CaptionAttribute[] atr = (CaptionAttribute[])(type.GetField(Enum.GetName(type, val))).GetCustomAttributes(typeof(CaptionAttribute), true);
                            string cap;
                            if (atr.Length > 0)
                                cap = GetLocalizedCaption(type, atr[0], val, cultureInfo);
                            else
                                cap = val.ToString();
    
                            n[val] = cap;
                        }
    
                        cult[cultureInfo] = n;
                    }
    
                    return n;
                }
            }
    
            private static ResourceManager GetResourceManager(Type type)
            {
                ResourceManager res;
    
                if (_managers.TryGetValue(type.Assembly, out res))
                    return res;
    
                lock (_managers)
                {
                    if (_managers.TryGetValue(type.Assembly, out res))
                        return res;
    
                    var names = type.Assembly.GetManifestResourceNames();
                    // вот только имя сборки и ее ресурсов это вопрос левый... решает не имя сборки, а неймспейс
                    //var assemblyName = type.Assembly.GetName().Name;
                    //var resName = assemblyName + "Properties.Resources.resources";
    
                    foreach (var name in names)
                    {
                        if (/*name.StartsWith(assemblyName, StringComparison.InvariantCultureIgnoreCase)
                            ||*/ name.EndsWith("Properties.Resources.resources", StringComparison.InvariantCultureIgnoreCase))
                        {
                            var n = name.Replace(".resources", "");
                            res = new ResourceManager(n, type.Assembly);
                            break;
                        }
                    }
                    _managers[type.Assembly] = res;
                }
    
                return res;
            }
    
            public static string EnumToString(object from)
            {
                LocalizedEnumConverter conv = new LocalizedEnumConverter(from.GetType());
                return (string)conv.ConvertTo(from, typeof(string));
            }
    
            public static T StringToEnum<T>(string value)
            {
                var conv = new LocalizedEnumConverter(typeof (T));
                return (T) conv.ConvertFromString(null, value);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that defines its own enum like this: public class Test
I have a class like that: public class Customer { public string Name {get;
I have an enum that looks like the following: public enum EnumWeapons { Fists
I have an enum that looks a little bit like this: public enum Numbers
I have an enum that I'd like to display all possible values of. Is
I have enum like this [Flags] public enum Key { None = 0, A
I have enum like this: public enum ObectTypes { TypeOne, TypeTwo, TypeThree, ... TypeTwenty
If I have an enum like this public enum Hungry { Somewhat, Very, CouldEatMySocks
Let's say you have an enum like this: public enum ColorsEnum { Undefined, Blue,
I have an enum type like this as an example: public Enum MyEnum {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.