issue when linking a TypeConverter to property

A

--== Alain ==--

Hi,

I 'm facing an interesting issue regarding a property and its TypeConverter.

When i do not attach a TypeConverter to this property, all custom
properties of my custom control are displayed in Test Container.

Since i attached this TypeConverter to my property, ALL custom
properties (properties implemented by myself) are not displayed in Test
Container.

therefore, i guess something is wrong with the TypeConverter or the way
how i attached it to my property.

here it is how i attached it :


[TypeConverter(typeof(CGridLineConverter))]
public CGridLine GridLines
{
...
}

and here is the typeconverter class i've developed ;

public class CGridLineConverter : ExpandableObjectConverter
{
/// <summary>
/// Check if it the parameter (context) is a string and can convert
it to class type (destinationType)
/// </summary>
/// <param name="context">string that holds the class type
information</param>
/// <param name="destinationType">class type to which the context
must be converted</param>
/// <returns>true if it's possible, or false in case of impossible
conversion</returns>
public override bool CanConvertTo(ITypeDescriptorContext
context,Type destinationType)
{
if (destinationType == typeof(string))
return true;
else
return base.CanConvertTo(context, destinationType);
}

/// <summary>
///
/// </summary>
/// <param name="context"></param>
/// <param name="culture"></param>
/// <param name="value"></param>
/// <param name="destinationType"></param>
/// <returns></returns>
public override object ConvertTo(ITypeDescriptorContext context,
CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(string))
return ConvertToString(value);
else
return base.ConvertTo(context, culture, value, destinationType);
}

/// <summary>
/// ConverToString whole subproperties
/// </summary>
/// <param name="value">value holds by CGridLine property</param>
/// <returns>Formatted string that holds the CGridLine property
converted into text</returns>
public string ConvertToString(object value)
{
CGridLine GridLine = (CGridLine)value;
ColorConverter ColorConverter = new ColorConverter();
return String.Format("{0}, {1}, {2}",
GridLine.Lines,
GridLine.Style,
ColorConverter.ConvertToString(GridLine.Color));
}

public override bool CanConvertFrom(ITypeDescriptorContext
context,Type sourceType)
{
if (sourceType == typeof(string))
return true;
else
return base.CanConvertFrom(context, sourceType);
}

public override object ConvertFrom(ITypeDescriptorContext context,
CultureInfo culture, object value)
{
if (value is string)
return ConvertFromString(value);
else
return base.ConvertFrom(context, culture, value);
}

public CGridLine ConvertFromString(object value)
{
string[] values = ((string)value).Split(',');
if (values.Length != 3)
throw new ArgumentException("Could not convert the value !");
try
{
CGridLine GridLine = new CGridLine();
ColorConverter ColorConverter = new ColorConverter();
StringConverter StringConverter = new StringConverter();

GridLine.Lines =
(GridLines)StringConverter.ConvertFromString(values[0]);
GridLine.Style =
(GridLineStyle)StringConverter.ConvertFromString(values[1]);
GridLine.Color =
(Color)ColorConverter.ConvertFromString(values[2]);

// Convert the name of the enumerated value into the corresponding
// enumerated value (which is actually an integer constant).

return GridLine;
}
catch (Exception err)
{
String ErrorMsg = "Could not convert the value \n\n Error
Message : " + err.Message;
//Debug.WriteLine("test");
throw new ArgumentException(ErrorMsg);
}
}
}


please, could you help me ?
thx.

Al.
 
A

--== Alain ==--

Hi,

I would like to add an another point.
I have the same problem if i develop my own property editor and attach
it to some class property.

All other properties (that i implemented) from this class, are not
displayed in the Test Container.

Is there a special step to do to make those things : TypeConverter and
TypeEditor ?

thanks.

Al.

--== Alain ==-- said:
Hi,

I 'm facing an interesting issue regarding a property and its
TypeConverter.

When i do not attach a TypeConverter to this property, all custom
properties of my custom control are displayed in Test Container.

Since i attached this TypeConverter to my property, ALL custom
properties (properties implemented by myself) are not displayed in Test
Container.

therefore, i guess something is wrong with the TypeConverter or the way
how i attached it to my property.

here it is how i attached it :


[TypeConverter(typeof(CGridLineConverter))]
public CGridLine GridLines
{
...
}

and here is the typeconverter class i've developed ;

public class CGridLineConverter : ExpandableObjectConverter
{
/// <summary>
/// Check if it the parameter (context) is a string and can convert
it to class type (destinationType)
/// </summary>
/// <param name="context">string that holds the class type
information</param>
/// <param name="destinationType">class type to which the context
must be converted</param>
/// <returns>true if it's possible, or false in case of impossible
conversion</returns>
public override bool CanConvertTo(ITypeDescriptorContext
context,Type destinationType)
{
if (destinationType == typeof(string))
return true;
else
return base.CanConvertTo(context, destinationType);
}

/// <summary>
///
/// </summary>
/// <param name="context"></param>
/// <param name="culture"></param>
/// <param name="value"></param>
/// <param name="destinationType"></param>
/// <returns></returns>
public override object ConvertTo(ITypeDescriptorContext context,
CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(string))
return ConvertToString(value);
else
return base.ConvertTo(context, culture, value, destinationType);
}

/// <summary>
/// ConverToString whole subproperties
/// </summary>
/// <param name="value">value holds by CGridLine property</param>
/// <returns>Formatted string that holds the CGridLine property
converted into text</returns>
public string ConvertToString(object value)
{
CGridLine GridLine = (CGridLine)value;
ColorConverter ColorConverter = new ColorConverter();
return String.Format("{0}, {1}, {2}",
GridLine.Lines,
GridLine.Style,
ColorConverter.ConvertToString(GridLine.Color));
}

public override bool CanConvertFrom(ITypeDescriptorContext
context,Type sourceType)
{
if (sourceType == typeof(string))
return true;
else
return base.CanConvertFrom(context, sourceType);
}

public override object ConvertFrom(ITypeDescriptorContext context,
CultureInfo culture, object value)
{
if (value is string)
return ConvertFromString(value);
else
return base.ConvertFrom(context, culture, value);
}

public CGridLine ConvertFromString(object value)
{
string[] values = ((string)value).Split(',');
if (values.Length != 3)
throw new ArgumentException("Could not convert the value !");
try
{
CGridLine GridLine = new CGridLine();
ColorConverter ColorConverter = new ColorConverter();
StringConverter StringConverter = new StringConverter();

GridLine.Lines =
(GridLines)StringConverter.ConvertFromString(values[0]);
GridLine.Style =
(GridLineStyle)StringConverter.ConvertFromString(values[1]);
GridLine.Color =
(Color)ColorConverter.ConvertFromString(values[2]);

// Convert the name of the enumerated value into the corresponding
// enumerated value (which is actually an integer constant).

return GridLine;
}
catch (Exception err)
{
String ErrorMsg = "Could not convert the value \n\n Error
Message : " + err.Message;
//Debug.WriteLine("test");
throw new ArgumentException(ErrorMsg);
}
}
}


please, could you help me ?
thx.

Al.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top