using System; using System.Collections.Specialized; using System.ComponentModel; using System.ComponentModel.Design.Serialization; using System.Reflection; namespace Spaanjaars.Controls { public class StringCollectionTypeConverter : TypeConverter { public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) { if (sourceType == typeof(string)) return true; else return base.CanConvertFrom(context, sourceType); } public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) { if ((destinationType == typeof(string)) | (destinationType == typeof(InstanceDescriptor))) return true; else return base.CanConvertTo(context, destinationType); } public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) { if (value != null) if (!(value is StringCollection)) throw new Exception("Wrong type. Sorry"); // convert to a string if (destinationType == typeof(string)) { // no value so we return an empty string if (value == null) return string.Empty; // strongly typed. Loop thhrough list and return a comma separated string string result = string.Empty; StringCollection tempList = value as StringCollection; for (int i = 0; i < tempList.Count; i++) { result += tempList[i]; if (i < tempList.Count - 1) { result += ","; } } return result; } // convert to a instance descriptor if (destinationType == typeof(InstanceDescriptor)) { // no value so we return no instance descriptor if (value == null) return null; // strongly typed StringCollection tempValue = value as StringCollection; // used to descripe the constructor MemberInfo Member = null; object[] Arguments = null; ConstructorInfo ctor = typeof(StringCollection).GetConstructor(new Type[] { }); // get the constructor for the type // value = new MyList() // string[] els = s.Split(new char[] { ',' }); // create the arguments to pass along // Retun a test list // string[] test = new string[] { "1", "3", "5" }; // No idea what to do here. // return the instance descriptor if (ctor != null) return new InstanceDescriptor(ctor, new object[] { }); else return null; } // call the base converter return base.ConvertTo(context, culture, value, destinationType); } public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) { if (value == null) return new StringCollection(); if (value is string) { string StringValue = value as string; if (StringValue.Length <= 0) return new StringCollection(); // create a new test list for now and see if that works. StringCollection tempList = new StringCollection(); tempList.Add("1"); tempList.Add("2"); tempList.Add("3"); tempList.Add("4"); return tempList; } // otherwise call the base converter else return base.ConvertFrom(context, culture, value); } } }