Windows Develop Bookmark and Share   
 index > Windows Forms Designer > Need some help with an Enum TypeConverter, I think
 

Need some help with an Enum TypeConverter, I think

Hello all,

I have a simple enumeration exposed as a public property in a component. I have defined Reset and ShouldSerialize methods but they are never getting called. The default RightClick menu shows a Reset item but when I click it I get an Error saying that Object type cannot be converted to target type.

To me, that seems to point to a type converter but I have not been having much luck getting one to work. Can someone point me or get me started? Thanks in advance for any ideas and/or suggestions!

j2associates  Monday, July 17, 2006 6:47 PM
Where's the code?
DMan1  Monday, July 17, 2006 6:50 PM

sorry, here it is

[code]

using System.IO;
using System.ComponentModel;
using System.Reflection;

namespace EnumTest
{

#region TestClass
public class ComponentTest : System.ComponentModel.Component
{

[TypeConverter(typeof(MyFamilyMembersTypeConverter))]
public enum MyFamilyMembers : int
{
Gary = 0,
Leslie,
Andy,
Mark,
Carrie,
Rachel
}

private const MyFamilyMembers m_DEFAULT = MyFamilyMembers.Rachel;

public ComponentTest()
{
base.New();
}

private MyFamilyMembers m_MyFamilyMember = GetDefaultMyFamilyMember();
[DefaultValue(m_DEFAULT)]
public MyFamilyMembers MyFamilyMember {
get {
return m_MyFamilyMember;
}
set {
m_MyFamilyMember = Value;
}
}

internal void ResetMyFamilyMember()
{
this.MyFamilyMember = GetDefaultMyFamilyMember();
}

internal bool ShouldSerializeMyFamilyMember()
{
return this.MyFamilyMember != GetDefaultMyFamilyMember;
}

private MyFamilyMembers GetDefaultMyFamilyMember()
{
return m_DEFAULT;
}
}
#endregion

#region TypeConverter

public class MyFamilyMembersTypeConverter : EnumConverter
{

public MyFamilyMembersTypeConverter()
{
base.New(typeof(ComponentTest.MyFamilyMembers));
}

public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
if (value is string) {
return Enum.Parse(typeof(ComponentTest.MyFamilyMembers), value, true);
}
return base.ConvertFrom(context, culture, value);
}

public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(string)) {
return Enum.GetName(typeof(ComponentTest.MyFamilyMembers), value);
}
return base.ConvertTo(context, culture, value, destinationType);
}
}
#endregion

}
[/code]

j2associates  Monday, July 17, 2006 10:12 PM
DefaultValue Attribute should be sufficient in this scenario. just drop your custom converter since i couldn't see any difference with the default EnumConverter
joeycalisay  Wednesday, July 19, 2006 12:40 PM

You can use google to search for other answers

Custom Search

More Threads

• How to show Multiple check boxes in TreeView Control.
• Combo box with treeview
• Children of a control incorrectly respond to the mouse popup menu interaction during design time.
• Default Control Modifier
• Inherited Toolstrips cannot be Designed
• What is wrong with MyDataset is nothing?
• Submitting to database
• Adding a dataset to DesignerHost
• How can I search the data in a dataset?
• Styles in windows controls