RE: PropertyGrid dynamic Dropdown List



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.

.



Relevant Pages

  • RE: browsable attribute broken with custom TypeConverter?
    ... GetProperties method provides a parameter attributes which already contains ... public override PropertyDescriptorCollection ... Microsoft Online Community Support ... where an initial response from the community or a Microsoft Support ...
    (microsoft.public.dotnet.framework.windowsforms.designtime)
  • RE: VS 2005 Classname Drop down
    ... In VB.NET 2005, to override a method/property in an inherited class, open ... member in the list. ... Microsoft Online Community Support ... where an initial response from the community or a Microsoft Support ...
    (microsoft.public.dotnet.general)
  • RE: Custom Collection Editor - how to block the up and down button
    ... float), but it's vital that it has some kind of sort order. ... protected override object CreateInstance ... Microsoft Online Community Support ... where an initial response from the community or a Microsoft Support ...
    (microsoft.public.dotnet.framework)
  • RE: Custom Collection Editor - how to block the up and down buttons ?
    ... I have spent several hours researching on this problem. ... I have tried to override the EditValue method. ... Microsoft Online Community Support ... where an initial response from the community or a Microsoft Support ...
    (microsoft.public.dotnet.framework)
  • Re: System Error (1003) - 0x000000CB
    ... MCSE, CCEA, Microsoft MVP - Terminal Server ... A call to Microsoft Support seems appropriate. ... Computer: TERMSERVER1 ...
    (microsoft.public.windows.terminal_services)