Wednesday, November 14, 2007

How to add ComboBox to PropertyGrid

Create new class which inherit StringCoverter, and 3 methods:

internal class List2PropertyConverter : StringConverter
{
public override bool
GetStandardValuesSupported(
ITypeDescriptorContext context)
{
//True - means show a Combobox
//and False for show a Modal
return true;
}

public override bool
GetStandardValuesExclusive(
ITypeDescriptorContext context)
{
//False - a option to edit values
//and True - set values to state readonly
return true;
}

public override StandardValuesCollection
GetStandardValues(
ITypeDescriptorContext context)
{
return new StandardValuesCollection(
new string[] {"test1", "test2", "..."});
}
}




Add variable and encapsulate it:
 private string testValue;
[Browsable(true)]
[Category("Category name")]
[TypeConverter(typeof(List2PropertyConverter))]
public string TestValues
{
get { return testValue; }
set { testValue = value; }
}




PropertyGrid has 4 attributes for a manage properties:


[Browsable(bool)] - Visible/Hide property

[ReadOnly(bool)] - Editing (true/false)

[Category(string)] - Group of properties

[Description(string)] - Description of the property

kick it on DotNetKicks.com

Regards,

5 comments:

Anonymous said...

Thanks! It was surprisingly hard to find a simple solution to this out there.

Anonymous said...

This sample is great and really hard to find. Thanks

Anonymous said...

Very helpful, Thank you.

Is there any chance someone could describe how to select/highlight an item from the ComboBox?

Many thanks 8~)

zeeeman said...

super helpful. Simple & useful. Thanks!

Anonymous said...

if I want to load at run time the list in the combobox's items, how can I do?