c# - TypeConverter for a List of Objects -
i'm trying create windows form uses property grid display list of objects. right have few classes. first one, displaycontainer, holds list of objects , instance of acts selected object property grid
[typeconverter(typeof(expandableobjectconverter))] public class displaycontainer { [categoryattribute("xrecord data")] public string name { get; set; } //name of collection uniquely id [typeconverter(typeof(expandableobjectconverter)), categoryattribute("xrecord data")] public list<object> objects { get; set; } //list of objects manipulated public displaycontainer() { objects = new list<object>(); } }
the object i'm wanting store instance of serialine class
[typeconverter(typeof(serialineconverter))] public class serialine { [categoryattribute("points")] public seriapoint firstpoint { get; set; } [categoryattribute("points")] public seriapoint secondpoint { get; set; } [categoryattribute("dimensions"), readonlyattribute(true)] public double length { get; set; } public serialine() { } public serialine(seriapoint passedfirstpoint, seriapoint passedsecondpoint) { firstpoint = passedfirstpoint; secondpoint = passedsecondpoint; updatelength(); } public void updatelength() { double = firstpoint.x - secondpoint.x; double b = firstpoint.y - secondpoint.y; double c = math.sqrt(math.pow(a, 2) + math.pow(b, 2)); length = c; } public override string tostring() { return "first point: " + firstpoint.tostring() + "; second point: " + secondpoint.tostring() + "; length: " + length.tostring() + ";"; } /// <summary> /// /// </summary> /// <param name="pointtoparse">string representation of point in form of (x, y, z)</param> public static seriapoint parsepoint(string pointtoparse) { string[] coordinates = pointtoparse.split(','); try { double xvalue = convert.todouble(coordinates[0]); double yvalue = convert.todouble(coordinates[1]); double zvalue = convert.todouble(coordinates[2]); seriapoint pointtoreturn = new seriapoint(xvalue, yvalue, zvalue); return pointtoreturn; } catch (formatexception) { return null; } } }
which uses converter
public class serialineconverter : expandableobjectconverter { public override bool canconvertto(itypedescriptorcontext context, system.type destinationtype) { if (destinationtype == typeof(serialine)) { return true; } return base.canconvertto(context, destinationtype); } public override object convertto(itypedescriptorcontext context, cultureinfo culture, object value, system.type destinationtype) { if (destinationtype == typeof(system.string) && value serialine) { serialine sl = (serialine)value; return "success: " + sl.firstpoint.tostring() + " " + sl.secondpoint.tostring() + " length " + sl.length; } return base.convertto(context, culture, value, destinationtype); } public override bool canconvertfrom(itypedescriptorcontext context, system.type sourcetype) { if (sourcetype == typeof(string)) return true; return base.canconvertfrom(context, sourcetype); } public override object convertfrom(itypedescriptorcontext context, cultureinfo culture, object value) { if (value string) { try { string s = (string)value; serialine sl = new serialine(); string[] parameters = s.split(';'); sl.firstpoint = serialine.parsepoint(parameters[0]); sl.secondpoint = serialine.parsepoint(parameters[1]); sl.length = convert.todouble(parameters[2]); } catch { throw new argumentexception("can not convert '" + (string)value + "' type serialine"); } } return base.convertfrom(context, culture, value); } }
and class points
[typeconverter(typeof(expandableobjectconverter))] public class seriapoint { public double x { get; set; } public double y { get; set; } public double z { get; set; } public seriapoint() { } public seriapoint(double passedx, double passedy, double passedz) { x = passedx; y = passedy; z = passedz; } public override string tostring() { return "(" + math.round(x, 2) + ", " + math.round(y, 2) + ", " + math.round(z, 2) + ")"; } }
now, i'm getting this:
but want dropping down menu show actual objects instead of capacity , count list. thought expandableobjectconverter put on list objects in displaycontainer apparently not.
Comments
Post a Comment