Pull an Object out of C# PropertyGrid with TypeConverter

306 views Asked by At

I'm trying to design a property grid which I can use to edit instances of my 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 a = 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 the 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;
            }
        }
    }

The Serialine class uses this class for 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) + ")";
        }
    }

And this TypeConverter to display properly on the property grid

    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 is Serialine)
            {
                Serialine sl = (Serialine)value; 
                return "Serialine at: " + sl.FirstPoint.ToString() + " to " + sl.SecondPoint.ToString() + " with 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 is 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 + "' to type Serialine");
                }
            }
            return base.ConvertFrom(context, culture, value);
        }
    }

Now, with the code above my Serialine class displays in a property grid like this:

enter image description here

With the Serialine class displayed and editable, I now need to grab the instance of the serialine class when it is changed so that my program is working with the most up to date version

    private void propertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
        {
            // ToDo: Serialine updatedLine = serialineShownInPropertyGridWithUpdatedValues
        }
0

There are 0 answers