BooleanConverter.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. //
  2. // System.ComponentModel.BooleanConverter
  3. //
  4. // Authors:
  5. // Martin Willemoes Hansen ([email protected])
  6. // Andreas Nahr ([email protected])
  7. //
  8. // (C) 2003 Martin Willemoes Hansen
  9. // (C) 2003 Andreas Nahr
  10. //
  11. using System.Globalization;
  12. namespace System.ComponentModel
  13. {
  14. public class BooleanConverter : TypeConverter
  15. {
  16. public BooleanConverter()
  17. {
  18. }
  19. public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
  20. {
  21. if (sourceType == typeof (string))
  22. return true;
  23. return base.CanConvertFrom (context, sourceType);
  24. }
  25. public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
  26. {
  27. if (value.GetType() == typeof (string)) {
  28. string Test = ((String) value).ToLower (culture);
  29. if (Test.Equals ("true"))
  30. return true;
  31. else if (Test.Equals ("false"))
  32. return false;
  33. else
  34. throw new FormatException ("No valid boolean value");
  35. }
  36. return base.ConvertFrom (context, culture, value);
  37. }
  38. public override StandardValuesCollection GetStandardValues (ITypeDescriptorContext context)
  39. {
  40. return new StandardValuesCollection (new bool[2] {true, false} );
  41. }
  42. public override bool GetStandardValuesExclusive (ITypeDescriptorContext context)
  43. {
  44. return true;
  45. }
  46. public override bool GetStandardValuesSupported (ITypeDescriptorContext context)
  47. {
  48. return true;
  49. }
  50. }
  51. }