Microsoft Communities

Welcome to WindowsClient.net | Sign in | Join

Here are some frequently asked questions about Windows Forms and their answers.

Windows Forms FAQs

How do I supply a list of values to be chosen from a drop-down list at runtime for a specific type similar to enums?

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.

using System.Globalization;

public class GridCellValueTypeConverter: TypeConverter 
{ 
  public override bool CanConvertFrom( ITypeDescriptorContext context,
    Type sourceType ) 
  { 
    return ( sourceType == typeof( string ) )
      ? true : base.CanConvertFrom(context,sourceType); 
  }
 
  public override object ConvertFrom( ITypeDescriptorContext context,
    CultureInfo culture, object value ) 
  { 
    if ( value is string ) 
    { 
      return ( ( (string) value) != string.Empty ) 
        ? Type.GetType( (string) value ) : 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; }

  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; 
}

Contributed from George Shepherd's Windows Forms FAQ



Page view counter