Another EnumConverter problem
-
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 -
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! IlanHi there, You should apply the
TypeConverter
attribute to theE1
property of theUserControl1
class instead of the enumE1
definition:public class UserControl1 : System.Windows.Forms.UserControl
{
...\[TypeConverter(typeof(E1C))\] public E1 E1 { ... } ...
}
However, IMO this question should belong to the C# forum.
-
Hi there, You should apply the
TypeConverter
attribute to theE1
property of theUserControl1
class instead of the enumE1
definition:public class UserControl1 : System.Windows.Forms.UserControl
{
...\[TypeConverter(typeof(E1C))\] public E1 E1 { ... } ...
}
However, IMO this question should belong to the C# forum.
-
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! IlanThis code is working fine with windows application but it is not working with web applicatioin. I am working with .net 3.5 with vs 2010. Can you please help me out here? You can also send me some sample code to ravick4u@yahoo.co.in Thanks