Windows Develop Bookmark and Share   
 index > Windows Forms General > How to change the display text to a custom one on propertygrid control
 

How to change the display text to a custom one on propertygrid control

Hi,

I am using the propertygrid to set config values for my winform app, written in C# 3.5. One of the properties of the class, which I use for the SelectedObject property of the control , is of type string array. So when the cntrol loads, it shows up as String [] Array. Is it possible to change itto a meaningful text such as "Select Fle Names"?

Any help is greatly appreciated.

Thanks

ThirstyMind  Monday, April 28, 2008 5:57 PM

Hi ThirstyMind,

A custom TypeConverter is not hard to implement. Generally we need toderive a class from the TypeConverter class and override the CanConvertTo, CanConvertFrom, ConvertTo and ConvertFrom methods. In your scenario, you only need to override the ConvertTo method.

The following is a sample:

class myclass
{
private string[] str;
[TypeConverter(typeof(MyConverter))]
public string[] Str
{
get { return str; }
set { str = value; }
}
}
class MyConverter : TypeConverter
{
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(string))
{
return "Select File Names";
}
return base.ConvertTo(context, culture, value, destinationType);
}
}

Hope this helps.

If you have any question, please feel free to let me know.

Linda

Linda Liu  Wednesday, April 30, 2008 3:36 PM
Hi,

Simply attach a new TypeConverter to your property and override ConvertTo to return your custom string.
Use Reflector to see how it is coded in the MultilineStringConverter for example.

Best regards,


Nicolas Cadilhac @ VisualHint
Smart PropertyGrid.Net
Microsoft PropertyGrid Resource List
Free PropertyGrid for MFC
Smart FieldPackEditor.Net / DateTimePicker

VisualHint  Monday, April 28, 2008 7:24 PM

I saw some samples about Typeconverters, but most of them are over my head . Would appreciate any simple code samples.

Thanks

ThirstyMind  Monday, April 28, 2008 10:00 PM

Hi ThirstyMind,

A custom TypeConverter is not hard to implement. Generally we need toderive a class from the TypeConverter class and override the CanConvertTo, CanConvertFrom, ConvertTo and ConvertFrom methods. In your scenario, you only need to override the ConvertTo method.

The following is a sample:

class myclass
{
private string[] str;
[TypeConverter(typeof(MyConverter))]
public string[] Str
{
get { return str; }
set { str = value; }
}
}
class MyConverter : TypeConverter
{
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(string))
{
return "Select File Names";
}
return base.ConvertTo(context, culture, value, destinationType);
}
}

Hope this helps.

If you have any question, please feel free to let me know.

Linda

Linda Liu  Wednesday, April 30, 2008 3:36 PM

Hi,

how can i add a checklist box or a checkbox to value column of the propertygrid.

chathurra

sharn722  Thursday, May 29, 2008 7:29 AM

Hi Chathurra,

You need to implement a custom UITypeEditor and then add a EditorAttribute with the custom UITypeEditor on the property you want.

For more information on UITypeEditor, please refer to the following MSDN document:

http://msdn.microsoft.com/en-us/library/system.drawing.design.uitypeeditor(VS.80).aspx

Hope this helps.

Thanks,

Linda

Linda Liu  Thursday, May 29, 2008 10:17 AM
This code sample resolved the problem. Than you so much

ThirstyMind  Sunday, June 15, 2008 6:11 AM

You can use google to search for other answers

Custom Search

More Threads

• DataGrid does not render unless I dock the dataGrid
• DateTimePicker initial date
• Creating custom border
• Adding a Rectangle object to a Panel
• Can't view "Now Playing" in Windows Media Player 10/11
• Order in DataGridView multiple select
• Custom Styles
• This forum IS NOT for Windows troubleshooting questions
• Check Box Size
• Converting a file to an UNKNOWN APPLICATION HELLP!!