Custom TypeConverter for a Class  
Author Message
Paw Jershauge





PostPosted: Visual C# General, Custom TypeConverter for a Class Top

Hi all.
My question is about making a custom TypeConverter. I have an example class showing under this text, where the user sees only 1,2 and 4 in a dropdown in the propertygrid.
What I want to do is this, insted of seeing the value 1,2 or 4 in a propertygrid, i want the user to see Low, Medium, High.
Where Low = 1, Medium = 2 and High = 4.
Can anyone show me a code to do so

public class PriorityConverter : ByteConverter
{

public
override bool GetStandardValuesSupported(ITypeDescriptorContext context)
{
return true;
}

public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
{
return false;
}

public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
return new StandardValuesCollection(new byte[]{1,2,4});
}

}


class MyClass
{
private int _PriorityAttribute = 0;

[TypeConverter(typeof(PriorityConverter)), Description("Get or Set the Priority of this class. Can be set to 1,2 or 4"), Category("Priority"), Browsable(true), ReadOnlyAttribute(false), DisplayName("Priority")]
public int PriorityAttribute
{
get
{
return _PriorityAttribute;
}
set
{
_PriorityAttribute = value;
}

}

}



Visual C#19  
 
 
GavH





PostPosted: Visual C# General, Custom TypeConverter for a Class Top

Hi Paw,

Could you not use an enumeration

enum Priority
{
Low =1,
Medium = 2,
High = 4
};

Then have PriorityAttribute as Type Priority (the enum) instead.

Regards



 
 
Paw Jershauge





PostPosted: Visual C# General, Custom TypeConverter for a Class Top

Hey GavH, thx for replying...

You are maybe right, didn’t think of that.

But the real class I’m working on, is data from a SQL Database, and i don’t really want to change the data type, as the data type is representing the data type of the field in the database.
So if I did what you are purposing, would that change anything

Best regards


 
 
Paw Jershauge





PostPosted: Visual C# General, Custom TypeConverter for a Class Top

Hey again GavH

Your suggestion is ok, and will work. The only thing is that i now have to convert the enum to a specified type, for the SQL command to work... but its better than nothing...
Thanks for the reply GavH... Answer accepted...