By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,375 Members | 1,365 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,375 IT Pros & Developers. It's quick & easy.

issue when linking a TypeConverter to property

P: n/a
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(value s[0]);
GridLine.Style =
(GridLineStyle)StringConverter.ConvertFromString(v alues[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.

Feb 10 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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 ==-- wrote:
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(value s[0]);
GridLine.Style =
(GridLineStyle)StringConverter.ConvertFromString(v alues[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.
Feb 11 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.