Live Chat Icon For mobile
Live Chat Icon

How can I supply a list of values to be chosen from a dropdown list at runtime for a specific type similar to enums?

Platform: WinForms| Category: Tips

You have to implement a TypeConverter for your class and override the GetStandardValues and GetStandardValuesSupported method. In your override of GetStandardValuesSupported you have to return true. In your override of GetStandardValues you should return the list of values.

Optionally you can override GetStandardValuesExclusive and allow the user to specify values that are not in the value list.

Note: The standard values collection can be initialized at runtime depending on the context of the instance object.


	public class GridCellValueTypeConverter: TypeConverter
	{
		public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)  
		{
			if (sourceType == typeof(System.String)) 
				return true;
			return base.CanConvertFrom(context,sourceType);
		}

		public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)  
		{
			if (value is System.String)
			{
				if (((string) value) != '') 
					return Type.GetType((string) value);
				else
					return null;
			}

			return base.ConvertFrom(context,culture,value);
		}

		// no string conversion
		public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)  
		{
			if (destinationType == typeof(String))
			{
				Type type = (Type) value;
				if (type == null)
					return String.Empty;
				else if (type.Namespace == 'System')
					return type.ToString();
				else
				{
					return String.Concat(type.FullName, ',', type.AssemblyQualifiedName.Split(’,’)[1]);
				}
			}

			return base.ConvertFrom(context,culture,value);

		}

		public override System.ComponentModel.TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)  
		{
			return svc;
		}

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

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

		// Fields
		static GridCellValueTypeConverter()
		{
			values = new string[] 
				{
					'System.String',
					'System.Double',
					'System.Int32',
					'System.Boolean',
					'System.DateTime',
					'System.Int16',
					'System.Int64',
					'System.Single',
					'System.Byte',
					'System.Char',
					'System.Decimal',
					'System.UInt16',
					'System.UInt32',
					'System.UInt64',
			};
			Array.Sort(values);
			Type[] types = new Type[values.Length];
			for (int i = 0; i < values.Length; i++)
				types[i] = Type.GetType(values[i]);
			svc = new TypeConverter.StandardValuesCollection(types);
		}

		private static string[] values;
		private static TypeConverter.StandardValuesCollection svc;
	}

Share with

Share on twitter
Share on facebook
Share on linkedin

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.