PropertyGrid dynamic Dropdown List

P

phcmi

I have a PropertyGrid question. My task is to replace a legacy dialog
box presentation with a modern one. The dialog itself allows the user
to set [key/value] configuration settings in our application, so it
seems to me that a PropertyGrid is a perfect modern replacement. I won't
bore you with the details with all the controls on the dialog since I'm
only concerned with one issue. The legacy dialog contains a drop down
list control that is initialized with the same overall values but these
values depend the context of an external object selected before the
dialog is instantiated.
For example:
If the user selects widget FOO then the drop down list contains values
such as COM1 and COM2.
However if the user selects widget BAR the drop down list contains
values such as COM3 and COM4.
Further, if the user selects widget FOOBAR the drop down list contains
values such as COM1, COM2, COM3 and COM4.
This is a common scenario in the dialog box, since there are 6 other
drop down list controls
that behave similarly.
By the way, I'm writing this in C# in VSNet 2005 and the Property Grid
control is the standard one available in the IDE. One minor addition, my
supervisor has specified that the PropertyGrid control strategy should
be used as the model for a number of other issues, so I'm committed to
using the PropertyGrid control for the time being.

Here's how we've chosen to solve this issue in a PropertyGrid.
I'm attempting to use the same TypeConverter derived class on a property
to initialize the drop down list control with these different values .
Below is a sample of the code:

public class myProp
{
string myStr;
[TypeConverter(typeof(MyConverter)]
public string MyItem
{
get { return myStr;}
set { myStr = value;}
}
}

public class MyConverter : StringConverter
{
}

Above, Property MyItem represents the TypeConverter object that will
manage the values COM1,COM2, COM3, etc.

The problem is that instead of copying and pasting a ton of code,
thereby creating many different classes and capturing multiple
permutations, I want to implement one class, namely the myProp class to
solve this issue elegantly.
However, since I can't pass any parameters to the MyItem property not
can I pass any parameters to the MyConverter class, I'm at a dead end.
Does the nature of the PropertyGrid's declarative attributes also mean
that data values are somewhat hard coded as well or is there a solution
to my quandary? Should I shelve the PropertyGrid approach?
Any ideas or new strategies would be greatly appreciated.
Thanks
 
V

VisualHint

Hello,

I hope that I understand the relationships between your classes...
Just wanted to propose something that could work for you. When your
Convert methods will be called in your TypeConverter you will get a
reference to a ITypeDescriptorContext. In there you have a property
called Instance that gives you a way to interrogate your myProp class
and ask it some informations like your selected widget. Does it fit
your design ?

Best regards,

Nicolas Cadilhac @ VisualHint (http://www.visualhint.com)
Home of Smart PropertyGrid for .Net and MFC
Microsoft PropertyGrid Resource List - http://
www.propertygridresourcelist.com


I have a PropertyGrid question. My task is to replace a legacy dialog
box presentation with a modern one. The dialog itself allows the user
to set [key/value] configuration settings in our application, so it
seems to me that a PropertyGrid is a perfect modern replacement. I won't
bore you with the details with all the controls on the dialog since I'm
only concerned with one issue. The legacy dialog contains a drop down
list control that is initialized with the same overall values but these
values depend the context of an external object selected before the
dialog is instantiated.
For example:
If the user selects widget FOO then the drop down list contains values
such as COM1 and COM2.
However if the user selects widget BAR the drop down list contains
values such as COM3 and COM4.
Further, if the user selects widget FOOBAR the drop down list contains
values such as COM1, COM2, COM3 and COM4.
This is a common scenario in the dialog box, since there are 6 other
drop down list controls
that behave similarly.
By the way, I'm writing this in C# in VSNet 2005 and the Property Grid
control is the standard one available in the IDE. One minor addition, my
supervisor has specified that the PropertyGrid control strategy should
be used as the model for a number of other issues, so I'm committed to
using the PropertyGrid control for the time being.

Here's how we've chosen to solve this issue in a PropertyGrid.
I'm attempting to use the same TypeConverter derived class on a property
to initialize the drop down list control with these different values .
Below is a sample of the code:

public class myProp
{
string myStr;
[TypeConverter(typeof(MyConverter)]
public string MyItem
{
get { return myStr;}
set { myStr = value;}
}

}

public class MyConverter : StringConverter
{

}

Above, Property MyItem represents the TypeConverter object that will
manage the values COM1,COM2, COM3, etc.

The problem is that instead of copying and pasting a ton of code,
thereby creating many different classes and capturing multiple
permutations, I want to implement one class, namely the myProp class to
solve this issue elegantly.
However, since I can't pass any parameters to the MyItem property not
can I pass any parameters to the MyConverter class, I'm at a dead end.
Does the nature of the PropertyGrid's declarative attributes also mean
that data values are somewhat hard coded as well or is there a solution
to my quandary? Should I shelve the PropertyGrid approach?
Any ideas or new strategies would be greatly appreciated.
Thanks
 
L

Linda Liu [MSFT]

Hi Phcmi,

I agree to what Nicolas has suggested.

When we override the methods, such as ConvertTo, ConvertFrom,
GetStandardValues and etc, in the derived TypeConverter class, we could get
the object that is connected with the type descriptor request through the
ITypeDescriptorContext parameter of the methods. So we could add a property
for the dynamic drop down list in the myProp class and then request it
later in the derived TypeConverter class.

The following is a sample. It requires that you add a PropertyGrid control
on the form in advance.

using System.ComponentModel;
using System.Drawing.Design;

public partial class Form1 : Form
{
public class myProp
{
string myStr;

[TypeConverter(typeof(MyConverter))]
public string MyItem
{
get { return myStr; }
set { myStr = value; }
}

List<string> list;

[Browsable(false)]
public List<string> MyList
{
get
{
if (list == null)
{
list = new List<string>();
list.Add("aaa");
list.Add("bbb");
list.Add("ccc");
}
return list;
}
}

}

private void Form1_Load(object sender, EventArgs e)
{
myProp obj = new myProp();
this.propertyGrid1.SelectedObject = obj;
}

}

class MyConverter : TypeConverter
{
public override bool
GetStandardValuesSupported(ITypeDescriptorContext context)
{
return true;
}
public override StandardValuesCollection
GetStandardValues(ITypeDescriptorContext context)
{
List<string> list = (context.Instance as Form1.myProp).MyList;
StandardValuesCollection cols = new
StandardValuesCollection(list);
return cols;
}
}

Hope this helps.
If you have any question, 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.
 
P

phcmi

Hi Nicolas and Linda,
Thank you both for your replies. Nicolas, your suggestion worked very
well. This is precisely the solution we were searching for.
Linda, thanks for the added extras as well.
Phcmi
Hi Phcmi,

I agree to what Nicolas has suggested.

When we override the methods, such as ConvertTo, ConvertFrom,
GetStandardValues and etc, in the derived TypeConverter class, we could get
the object that is connected with the type descriptor request through the
ITypeDescriptorContext parameter of the methods. So we could add a property
for the dynamic drop down list in the myProp class and then request it
later in the derived TypeConverter class.

The following is a sample. It requires that you add a PropertyGrid control
on the form in advance.

using System.ComponentModel;
using System.Drawing.Design;

public partial class Form1 : Form
{
public class myProp
{
string myStr;

[TypeConverter(typeof(MyConverter))]
public string MyItem
{
get { return myStr; }
set { myStr = value; }
}

List<string> list;

[Browsable(false)]
public List<string> MyList
{
get
{
if (list == null)
{
list = new List<string>();
list.Add("aaa");
list.Add("bbb");
list.Add("ccc");
}
return list;
}
}

}

private void Form1_Load(object sender, EventArgs e)
{
myProp obj = new myProp();
this.propertyGrid1.SelectedObject = obj;
}

}

class MyConverter : TypeConverter
{
public override bool
GetStandardValuesSupported(ITypeDescriptorContext context)
{
return true;
}
public override StandardValuesCollection
GetStandardValues(ITypeDescriptorContext context)
{
List<string> list = (context.Instance as Form1.myProp).MyList;
StandardValuesCollection cols = new
StandardValuesCollection(list);
return cols;
}
}

Hope this helps.
If you have any question, 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.
 
P

phcmi

Hi Nicolas and Linda,
Thank you both for your replies. Nicolas, your suggestion worked very
well. This is precisely the solution we were searching for.
Linda, thanks for the added extras as well.
Phcmi
Hi Phcmi,

I agree to what Nicolas has suggested.

When we override the methods, such as ConvertTo, ConvertFrom,
GetStandardValues and etc, in the derived TypeConverter class, we could get
the object that is connected with the type descriptor request through the
ITypeDescriptorContext parameter of the methods. So we could add a property
for the dynamic drop down list in the myProp class and then request it
later in the derived TypeConverter class.

The following is a sample. It requires that you add a PropertyGrid control
on the form in advance.

using System.ComponentModel;
using System.Drawing.Design;

public partial class Form1 : Form
{
public class myProp
{
string myStr;

[TypeConverter(typeof(MyConverter))]
public string MyItem
{
get { return myStr; }
set { myStr = value; }
}

List<string> list;

[Browsable(false)]
public List<string> MyList
{
get
{
if (list == null)
{
list = new List<string>();
list.Add("aaa");
list.Add("bbb");
list.Add("ccc");
}
return list;
}
}

}

private void Form1_Load(object sender, EventArgs e)
{
myProp obj = new myProp();
this.propertyGrid1.SelectedObject = obj;
}

}

class MyConverter : TypeConverter
{
public override bool
GetStandardValuesSupported(ITypeDescriptorContext context)
{
return true;
}
public override StandardValuesCollection
GetStandardValues(ITypeDescriptorContext context)
{
List<string> list = (context.Instance as Form1.myProp).MyList;
StandardValuesCollection cols = new
StandardValuesCollection(list);
return cols;
}
}

Hope this helps.
If you have any question, 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.
 

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