Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. EnumConverter problem

EnumConverter problem

Scheduled Pinned Locked Moved C#
csharphelp
2 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    e laj
    wrote on last edited by
    #1

    I have the following enum:

    \[TypeConverter(typeof(E1C))\]
    public enum E1
    {
    	a = 1,
    	b = 2,
    	c = 3
    }
    

    And the following converter:

    public class E1C : EnumConverter
    {
    	public E1C(): base(typeof(E1))
    	{
    	}
    	public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
    	{
    		if(value != null)
    		{
    			if(destinationType == typeof(string))
    			{
    				if((E1)value == E1.a)
    				{
    					return "a a a";
    				}
    				else if((E1)value == E1.b)
    				{
    					return "b b b";
    				}
    				else if((E1)value == E1.c)
    				{
    					return "c c c";
    				}
    			}
    			if(destinationType == typeof(InstanceDescriptor))
    			{
    				FieldInfo fi = EnumType.GetField(value.ToString());
    				if(fi != null)
    				{
    					return new InstanceDescriptor(fi, null);
    				}
    			}
    		}
    		return base.ConvertTo (context, culture, value, destinationType);
    	}
    
    	public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
    	{
    		if(value != null && value is string)
    		{
    			if((string) value == "a a a")
    			{
    				return E1.a;
    			}
    			else if((string) value == "b b b")
    			{
    				return E1.b;
    			}
    			else if((string) value == "c c c")
    			{
    				return E1.c;
    			}
    		}
    		return base.ConvertFrom (context, culture, value);
    	}
    }
    

    Now, on the property page every thing works fine, as well as in ASP .net page, but within Windows Form, the designer generates the folowing code:

    	#region Windows Form Designer generated code
    	...
    	...		
    	this.userControl11.E1 = WindowsApplication2.E1.b b b;
    	...
    	...
    	#endregion
    

    As you can see, instead of generating: **this.userControl11.E1 = WindowsApplication2.E1.b;** It generated: **this.userControl11.E1 = WindowsApplication2.E1.b b b;** If anyone know this problem and its solution, it would be nice! Thanks in advance! Ilan

    E 1 Reply Last reply
    0
    • E e laj

      I have the following enum:

      \[TypeConverter(typeof(E1C))\]
      public enum E1
      {
      	a = 1,
      	b = 2,
      	c = 3
      }
      

      And the following converter:

      public class E1C : EnumConverter
      {
      	public E1C(): base(typeof(E1))
      	{
      	}
      	public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
      	{
      		if(value != null)
      		{
      			if(destinationType == typeof(string))
      			{
      				if((E1)value == E1.a)
      				{
      					return "a a a";
      				}
      				else if((E1)value == E1.b)
      				{
      					return "b b b";
      				}
      				else if((E1)value == E1.c)
      				{
      					return "c c c";
      				}
      			}
      			if(destinationType == typeof(InstanceDescriptor))
      			{
      				FieldInfo fi = EnumType.GetField(value.ToString());
      				if(fi != null)
      				{
      					return new InstanceDescriptor(fi, null);
      				}
      			}
      		}
      		return base.ConvertTo (context, culture, value, destinationType);
      	}
      
      	public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
      	{
      		if(value != null && value is string)
      		{
      			if((string) value == "a a a")
      			{
      				return E1.a;
      			}
      			else if((string) value == "b b b")
      			{
      				return E1.b;
      			}
      			else if((string) value == "c c c")
      			{
      				return E1.c;
      			}
      		}
      		return base.ConvertFrom (context, culture, value);
      	}
      }
      

      Now, on the property page every thing works fine, as well as in ASP .net page, but within Windows Form, the designer generates the folowing code:

      	#region Windows Form Designer generated code
      	...
      	...		
      	this.userControl11.E1 = WindowsApplication2.E1.b b b;
      	...
      	...
      	#endregion
      

      As you can see, instead of generating: **this.userControl11.E1 = WindowsApplication2.E1.b;** It generated: **this.userControl11.E1 = WindowsApplication2.E1.b b b;** If anyone know this problem and its solution, it would be nice! Thanks in advance! Ilan

      E Offline
      E Offline
      e laj
      wrote on last edited by
      #2

      I posted this message in ASP.net by mistake. However, minhpc_bk has answer this question, and it works fine! Here is the link to the answer of the same question (if anybody is interested). The answer

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups