Jimmer
Tue Aug 15 05:19:02 CDT 2006
Linda,
Thanks for the reply. Problem is that when multiple objects are added to a
property grid I need to get the value of each object per property row, or in
other words, every time GetStandardValues is called i need the value of
everything in the context.instance array. So what I need is something like
(modified from your code) ---
if (context.Instance is Array)
{
for (int i = 0; i < ((object[])context.Instance).Length; i++)
{
object myobj =
context.PropertyDescriptor.GetValue(((object[])context.Instance)[i]);
//Statement A
}
}
but statement A won't work as the propertyDescriptor is a
mergepropertydescriptor type and the GetValue ftn only takes an object array.
Therefore I would have expected ...
object[] myobj = context.PropertyDesciptor.GetValue(context.Instance);
to work fine, but it doesn't as GetValue returns an object not object[].
(and returns null if assigned to an object type)
I hope this clarifies the problem.
Again, thanks in advance for your help.
Jimmer
"Linda Liu [MSFT]" wrote:
> Hi Jimmer,
>
> I performed a test based on your sample code and did see the problem.
>
> I derive a new class named MyConverter from TypeConverter. The following is
> the code in MyConverter class.
>
> using System.ComponentModel;
> public class MyConverter : TypeConverter
> {
> public override bool
> GetStandardValuesSupported(ITypeDescriptorContext context)
> {
> return true;
> }
> public override TypeConverter.StandardValuesCollection
> GetStandardValues(ITypeDescriptorContext context)
> {
> object myobj =
> context.PropertyDescriptor.GetValue(context.Instance); //
> statement3: get the current displayed object
> return new StandardValuesCollection(new String[] {
> "string1", "string2", "string3" });
> }
> }
>
> Then I apply MyConverter to MyClass like below.
> public class MyClass
> {
> private string name;
> private int grade;
>
> [TypeConverter(typeof(MyConverter))]
> public string Name
> {
> get { return name;}
> set { name = value;}
> }
>
> public int Grade
> {
> get { return grade;}
> set { grade = value;}
> }
> }
>
> I drag&drop a PropertyGrid onto the form and add the following code in the
> form's Load event handler.
> private void Form1_Load(object sender, EventArgs e)
> {
> MyClass ins1 = new MyClass();
> ins1.Name = "this is my name";
>
> MainClass ins2 = new MainClass();
> object [] objs = {ins1,ins2};
>
> this.propertyGrid1.SelectedObjects = objs; // statement1: add
> several objects to the propertyGrid1
> this.propertyGrid1.SelectedObject = ins1; // statement2:
> add one object to the propertyGrid1
> }
>
> If I comment out the statement1, the statement3 in the overridden
> GetStandardValues method in the MyConverter class returns the string
> variable correctly when the program is running. However, if I comment out
> the statement2, the statement3 returns null. I set a break point on the
> statement3 and I see the type of the statement "context.Instance" is
> "object[ ]" when I add several objects to the PropertyGrid. This is the
> reason why the statement3 returns null in this case. We should enumerate
> the elements in the array and get the current displayed objects one by one.
>
> The following is a sample for this.
>
> public override TypeConverter.StandardValuesCollection
> GetStandardValues(ITypeDescriptorContext context)
> {
> if (context.Instance is object)
> {
> object myobj =
> context.PropertyDescriptor.GetValue(context.Instance);
> }
> else if (context.Instance is object[])
> {
> for (int i = 0; i < ((object[])context.Instance).Length;
> i++)
> {
> object myobj = ((object[])context.Instance)[i];
>
> }
> }
> return new StandardValuesCollection(new String[] { "string1",
> "string2", "string3" });
> }
>
> Hope this helps.
>
> If you have anything unclear or concerns, please feel free to let me know.
>
> Sincerely,
> Linda Liu
> Microsoft Online Community Support
>
> ==================================================
> Get notification to my posts through email? Please refer to
>
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
> ications.
>
> Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
> where an initial response from the community or a Microsoft Support
> Engineer within 1 business day is acceptable. Please note that each follow
> up response may take approximately 2 business days as the support
> professional working with you may need further investigation to reach the
> most efficient resolution. The offering is not appropriate for situations
> that require urgent, real-time or phone-based interactions or complex
> project analysis and dump analysis issues. Issues of this nature are best
> handled working with a dedicated Microsoft Support Engineer by contacting
> Microsoft Customer Support Services (CSS) at
>
http://msdn.microsoft.com/subscriptions/support/default.aspx.
> ==================================================
>
> This posting is provided "AS IS" with no warranties, and confers no rights.
>
>