PropertyGrid values in hex/hexadecimal using class derived from TypeConverter


/ Published in: C#
Save to your folder(s)

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


Copy this code and paste it in your HTML
  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 ;

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.