Display Yes/No instead of True/False

The property grid is very versatile and using a few attributes and some simple code we can change its default behaviour and enhance it to suit most property editing situations.

A commonly requested feature is to enable PropertyGrid to display and edit "Yes" and "No" instead of "True" and "False". This can be accomplished by using a simple type converter.

The code basically needs to replace the BooleanTypeConverter which converts between logical true and false values and the strings "True" and "false". In our replacement the values of true and false are associated with the strings "Yes" and "No". It's important to provide a set of standard values in this case. This is accomplished by responding to the GetStandardValuesSupported and GetStandardValues methods.

The TypeConverter that does this is shown in the following listing

  public class YesNoTypeConverter : TypeConverter

  {

    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)

    {

      if(sourceType == typeof(string))

        return true;

      return base.CanConvertFrom (context, sourceType);

    }

 

    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)

    {

      if(destinationType == typeof(string))

        return true;

      return base.CanConvertTo (context, destinationType);

    }

 

    public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)

    {

      if(value.GetType() == typeof(string))

      {

        if(((string)value).ToLower()=="yes")

          return true;

        if(((string)value).ToLower()=="no")

          return false;

        throw new Exception("Values must be \"Yes\" or \"No\"");

      }

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

    }

 

    public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)

    {

      if(destinationType == typeof(string))

      {

        return (((bool)value) ? "Yes" : "No");

      }

      return base.ConvertTo (context, culture, value, destinationType);

    }

 

    public override bool GetStandardValuesSupported(ITypeDescriptorContext context)

    {

      return true;

    }

 

    public override System.ComponentModel.TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)

    {

      bool[] bools=new bool[]{true,false};

      System.ComponentModel.TypeConverter.StandardValuesCollection svc=new System.ComponentModel.TypeConverter.StandardValuesCollection(bools);

      return svc;

    }

 

  }

 

Here is the same TypeConverter in Visual Basic

    <TypeConverter(GetType(YesNoTypeConverter))>  _

    Public Property [Boolean]() As Boolean

     Get

      Return _boolean

     End Get

     Set

      _boolean = value

     End Set

    End Property

   End Class 'CheckBoxInPropertyGrid

  

  

   Public Class YesNoTypeConverter

    Inherits TypeConverter

    

    Public Overloads Overrides Function CanConvertFrom(ByVal context As ITypeDescriptorContext, ByVal sourceType As Type) As Boolean

      If sourceType Is GetType(String) Then

        Return True

      End If

      Return MyBase.CanConvertFrom(context, sourceType)

    End Function 'CanConvertFrom

 

 

    Public Overloads Overrides Function CanConvertTo(ByVal context As ITypeDescriptorContext, ByVal destinationType As Type) As Boolean

      If destinationType Is GetType(String) Then

        Return True

      End If

      Return MyBase.CanConvertTo(context, destinationType)

    End Function 'CanConvertTo

 

 

    Public Overloads Overrides Function ConvertFrom(ByVal context As ITypeDescriptorContext, ByVal culture As System.Globalization.CultureInfo, ByVal value As Object) As Object

      If value.GetType() Is GetType(String) Then

        If CStr(value).ToLower() = "yes" Then

          Return True

        End If

        If CStr(value).ToLower() = "no" Then

          Return False

        End If

        Throw New Exception("Values must be ""Yes"" or ""No""")

      End If

      Return MyBase.ConvertFrom(context, culture, value)

    End Function 'ConvertFrom

 

 

    Public Overloads Overrides Function ConvertTo(ByVal context As ITypeDescriptorContext, ByVal culture As System.Globalization.CultureInfo, ByVal value As Object, ByVal destinationType As Type) As Object

      If destinationType Is GetType(String) Then

        Return IIf(CBool(value), "Yes", "No")

      End If

      Return MyBase.ConvertTo(context, culture, value, destinationType)

    End Function 'ConvertTo

 

 

    Public Overloads Overrides Function GetStandardValuesSupported(ByVal context As ITypeDescriptorContext) As Boolean

      Return True

    End Function 'GetStandardValuesSupported

 

 

    Public Overloads Overrides Function GetStandardValues(ByVal context As ITypeDescriptorContext) As System.ComponentModel.TypeConverter.StandardValuesCollection

      Dim bools() As Boolean = {True, False}

      Dim svc As New System.ComponentModel.TypeConverter.StandardValuesCollection(bools)

      Return svc

    End Function 'GetStandardValues

  End Class 'YesNoTypeConverter

 

This TypeConverter can be applied to any Boolean value using the TypeConverterAttribute and the value will be displayed in the Property Grid as Yes or No, you will be able to enter the values from the keyboard in longhand and the dropdown will show the same.

Return to the Tips and Tricks page.