118 lines
2.8 KiB
C#

using System;
using System.ComponentModel;
using System.Globalization;
namespace DevComponents.Charts.Design
{
/// <summary>
/// Represents PointValueConverter converter.
/// </summary>
public class PointValueConverter : TypeConverter
{
public PointValueConverter()
{
}
#region CanConvertTo
public override bool CanConvertFrom(
ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string))
return true;
return base.CanConvertFrom(context, sourceType);
}
#endregion
#region CanConvertTo
public override bool CanConvertTo(
ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(string))
return true;
return base.CanConvertTo(context, destinationType);
}
#endregion
#region ConvertFrom
public override object ConvertFrom(
ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is string)
return (GetConvertedValue((string)value));
return base.ConvertFrom(context, culture, value);
}
#region GetConvertedValue
private object GetConvertedValue(string text)
{
string s = text.Trim();
if (text.Length == 0)
return (null);
if (s.StartsWith("\"") && s.EndsWith("\""))
return (s.Substring(1, s.Length - 2));
int intResult;
if (int.TryParse(text, out intResult) == true)
return (intResult);
double dblResult;
if (double.TryParse(text, out dblResult) == true)
return (dblResult);
DateTime dtResult;
if (DateTime.TryParse(text, out dtResult) == true)
return (dtResult);
return (text);
}
#endregion
#endregion
#region ConvertTo
public override object ConvertTo(
ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (value != null)
{
if (destinationType == typeof(string))
{
if (value == null)
return ("<null>");
if (value is string)
return ("\"" + value + "\"");
if (value is int)
return (value.ToString());
if (value is DateTime)
return (value.ToString());
return (String.Format("{0:0.0###############}", value));
}
}
return (string.Empty);
}
#endregion
}
}