using System; using System.Collections.Generic; using System.Text; using System.ComponentModel; using System.Globalization; using System.ComponentModel.Design.Serialization; using System.Reflection; namespace Grafikon { public class IntervalConverter : ExpandableObjectConverter { public override bool CanConvertTo(ITypeDescriptorContext context, System.Type destinationType) { if (destinationType == typeof(cInterval)) return true; if (destinationType == typeof(InstanceDescriptor)) 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 cInterval)) { cInterval i = (cInterval)value; String s = String.Format("{0}; {1}", i.Min, i.Max); return s; } if ((destinationType == typeof(InstanceDescriptor) && (value is cInterval))) { cInterval i = (cInterval)value; Object[] Properties = new Object[2]; Type[] Types = new Type[2]; Types[0] = typeof(float); Types[1] = typeof(float); Properties[0] = i.Min; Properties[1] = i.Max; ConstructorInfo ci = typeof(cInterval).GetConstructor(Types); if (ci == null) throw new Exception("FASZ"); return new InstanceDescriptor(ci, Properties, true); } 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; int semicolon = s.IndexOf(';'); if (semicolon != -1) { float min = Single.Parse(s.Substring(0, semicolon)); float max = Single.Parse(s.Substring(semicolon + 1)); cInterval i = new cInterval(min, max); return i; } } catch { throw new ArgumentException( "Can not convert '" + (string)value + "' to type SpellingOptions"); } } return base.ConvertFrom(context, culture, value); } } [TypeConverterAttribute(typeof(IntervalConverter))] public struct cInterval { private float _min, _max; public float Min { get { return _min; } set { _min = value; } } public float Max { get { return _max; } set { _max = value; } } public cInterval(float min, float max) { _min = min; _max = max; } } }