Property grid ExpandableObjectConverter DoubleClick problem.

V

Vladimir.Sakharuk

I have created ExpandableObjectConverter in C#. for use in
System.Windows.Forms.PropertyGrid.
The property grid shows the DiffItem is a object that holds member
TheItem.

without expanding TheItem
It works if u click, select from drop down box. until you DoubleClick
it shows error:
"Invalid property Value". with "Object type cannot be converted to
target type."

Here is a snapshot of code:

public class ItemConverter:ExpandableObjectConverter
{
public override bool CanConvertTo(ITypeDescriptorContext context, Type
destinationType)
{
if(destinationType == typeof(TheItem)) return true;
return base.CanConvertTo (context, destinationType);


}

public override object ConvertTo(ITypeDescriptorContext context,
CultureInfo culture, object value, Type destinationType)
{
if(destinationType == typeof(System.String) && value is TheItem)
{
return ((TheItem)value).Rating;
}

object ret = base.ConvertTo (context, culture, value, destinationType);

//Line 1
return ret;
}


public override bool GetStandardValuesSupported( ITypeDescriptorContext

context)
{
return true;

}


public override bool GetStandardValuesExclusive( ITypeDescriptorContext

context)
{
return true;

}


public override StandardValuesCollection
GetStandardValues(ITypeDescriptorContext context)
{
if( context.Instance is DiffItem )
{
//Line 2
return new StandardValuesCollection(new string[]
{"N/R","A","B","C","D","E"});

}


return base.GetStandardValues (context);


}


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

}


public override object ConvertFrom(ITypeDescriptorContext context,
CultureInfo culture, object value)
{
if( value is string )
{
if(context.Instance is TheItem)
{
TheItem item = new TheItem((string) value,DateTime.Now);
return item;

}


if( context.Instance is DiffItem )
{
DiffItem diff = (DiffItem )context.Instance;
TheItem rating=diff.Rating;
TheItem item = new TheItem ((string) value,rating.theDate);
return item;

}
}


return base.ConvertFrom (context, culture, value);


}
}


//the Item class
[TypeConverterAttribute(typeof(ItemConverter))]
public class TheItem
{
string rating="N/A";
DateTime theDate=DateTime.Min;
#region Constructors
public TheItem()
{}

public TheItem(string value, DateTime thedate )
{
this.rating=value;
this.theDate=thedate ;


}


#endregion

#region Properties


[DescriptionAttribute("The Rating itself."),
DefaultValueAttribute("N/A"),
RefreshPropertiesAttribute(RefreshProperties.All),
ReadOnlyAttribute(false)]
public string Rating
{
get{return rating;}
set{rating=value;}


}


[DescriptionAttribute("Rating Date."),
ReadOnlyAttribute(false)]
public DateTime RatingDate
{
get{return theDate;}
set{theDate=value;}


}
#endregion
}
}


where DiffItem is a object that holds member TheItem:
[CategoryAttribute("Ratings"),
ReadOnlyAttribute(false)]

public TheItem TheRating
{
get { return rating; }
set { rating=value; }


}


I have debug the code...
it goes to //line 2 (see the code)
and gets the values.
then it goes to //line 1
and loop trough all possible values.
The again goes to //line 2 and then shows the error.
Any help how to fix this welcome.
Thank you \/|@d.

Just posting in the right group.
 
V

Vladimir.Sakharuk

I got the same error (when you Double Click the item) on System
standard System.Drawing.Color when used as a property in C#.
I have Windows XP Service Pack 2 and Microsoft DOTNET 2.
 

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