Posted By

jimfred on 07/30/09


Tagged

c TypeConverter


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

umang_nine


PropertyGrid values in hex/hexadecimal using class derived from TypeConverter


 / Published in: C#
 

URL: http://koniosis.blogspot.com/2009/02/integers-as-hex-in-propertygrid-c-net.html

Displaying values as hex in a PropertyGrid by using a TypeConverter class. The TypeConverter class is assigned as a property to the PropertyGrid's data element.

Consider improving by making the UInt32HexTypeConverter a generic/template as in HexTypeConverter. Also, consider deriving from BaseNumberConverter .

See 'How to: Implement a Type Converter' in MSDN

  1. // Included...
  2. // [1] Define TypeConverter class for hex.
  3. // [2] use it when defining a member of a data class using attribute TypeConverter.
  4. // [3] Connect the data class to a PropertyGrid.
  5.  
  6.  
  7. // [1] define UInt32HexTypeConverter is-a TypeConverter
  8. public class UInt32HexTypeConverter : TypeConverter
  9. {
  10. public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
  11. {
  12. if (sourceType == typeof(string))
  13. {
  14. return true;
  15. }
  16. else
  17. {
  18. return base.CanConvertFrom(context, sourceType);
  19. }
  20. }
  21.  
  22. public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
  23. {
  24. if (destinationType == typeof(string))
  25. {
  26. return true;
  27. }
  28. else
  29. {
  30. return base.CanConvertTo(context, destinationType);
  31. }
  32. }
  33.  
  34. public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
  35. {
  36. if (destinationType == typeof(string) && value.GetType() == typeof(UInt32))
  37. {
  38. return string.Format("0x{0:X8}", value);
  39. }
  40. else
  41. {
  42. return base.ConvertTo(context, culture, value, destinationType);
  43. }
  44. }
  45.  
  46. public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
  47. {
  48. if (value.GetType() == typeof(string))
  49. {
  50. string input = (string)value;
  51.  
  52. if (input.StartsWith("0x", StringComparison.OrdinalIgnoreCase))
  53. {
  54. input = input.Substring(2);
  55. }
  56.  
  57. return UInt32.Parse(input, System.Globalization.NumberStyles.HexNumber, culture);
  58. }
  59. else
  60. {
  61. return base.ConvertFrom(context, culture, value);
  62. }
  63. }
  64. } // UInt32HexTypeConverter used by grid to display hex.
  65.  
  66.  
  67. // [2] define a class for data to be associated with the PropertyGrid.
  68. [DefaultPropertyAttribute("Name")]
  69. public class Data
  70. .
  71. .
  72. .
  73. public UInt32 stat;
  74. [CategoryAttribute("Main Scanner"), DescriptionAttribute("Status"), TypeConverter(typeof(UInt32HexTypeConverter))]
  75. public UInt32 Status
  76. {
  77. get { return stat; }
  78. }
  79. .
  80. .
  81. .
  82. // [3] reference data for propertyGrid. (here, myData is a Data).
  83. propertyGrid1.SelectedObject = myData ;

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: koniosis on December 21, 2010

Hi, I think it is worth pointing out that my original post (http://koniosis.blogspot.com/2009/02/integers-as-hex-in-propertygrid-c-net.html) allows any integer property of any object displayed in the PropertyGrid to be shown in Hex without the need to add Attributes. This also allows the hex display to be toggled at runtime as required.

I'd be interested to see a variant that uses the BaseNumberConverter or derived classes.

Thanks

You need to login to post a comment.