Windows Develop Bookmark and Share   
 index > Windows Forms Designer > Adding Attributes to Properties At Runtime in C#...
 

Adding Attributes to Properties At Runtime in C#...

Hi !

Currently I'm doing an MDI application which has the Toolbox, PropertyGrid and the DesignSurfaces. i'm using some third-party controls as the controls available in the Toolbox.

This application will function similar to a VS Editor where drag and drop of controls, modifying the properites of the various control instances, adding more Tabpages, saving the Templates that was created and many more....

I have an issue in using Attributes for the Properites of the different controls.
I am using a Custom ControlDesigner to filter out certain properties and adding some properties.

while filtering out properties,i use the BrowsableAttribute class as:

protected override void

PreFilterProperties(System.Collections.IDictionary properties)
{

base.PreFilterProperties(properties);
string[] propertiesNotToShow = {
"AutoCenter", "AutoFrameRate", "AutoScaleEnabled", "AutoScaleStyle", "BackGroundPicture","BorderStyle", "CachedDrawing", "ComponentHandle"
} //i have some 60 odd properties

foreach (string propname in propertiesNotToShow)
{
prop = (PropertyDescriptor)properties[propname];
if (prop != null)
{
AttributeCollection runtimeAttributes = prop.Attributes;
// make a copy of the original attributes
// but make room for one extra attribute
Attribute[] attrs = new Attribute[runtimeAttributes.Count + 1];
runtimeAttributes.CopyTo(attrs, 0);
attrs[runtimeAttributes.Count] = new BrowsableAttribute(false);

// makes this Property hidden in PropertyGrid
prop = TypeDescriptor.CreateProperty(this.GetType(), propname, prop.PropertyType, attrs);
properties[propname] = prop;
}
}
}

In the same approach i am not able to use the other Attributes like CategoryAttribute, DefaultValueAttribute and DesignerSerializationAttribute . Though i dont get a compile-time error, during runtime in the PropertyGrid, instead of showing the values, i get an error "Object reference not set to an instance of an object".

Can anyone please explain why this happens with these set of attributes and for BrowsableAttribute it works fine....

Rajesh_2959  Friday, May 25, 2007 1:22 PM
Typo: change prop = TypeDescriptor.CreateProperty(this.GetType(), propname, prop.PropertyType, attrs); to prop = TypeDescriptor.CreateProperty(control.GetType(), propname, prop.PropertyType, attrs);

Marc.
Malacki  Friday, May 25, 2007 6:55 PM
Typo: change prop = TypeDescriptor.CreateProperty(this.GetType(), propname, prop.PropertyType, attrs); to prop = TypeDescriptor.CreateProperty(control.GetType(), propname, prop.PropertyType, attrs);

Marc.
Malacki  Friday, May 25, 2007 6:55 PM

thanks a lot Malacki !!

It has worked :-)

thanks for your time.......

rajesh

Rajesh_2959  Monday, May 28, 2007 1:32 PM

Hi rajesh,

I've to fix the same problem as you did in 2007.

When I try to use the same solution as you I get the error message

"property or indexer 'System.ComponentModel.PropertyDescriptorCollection.this[string] cannot be assigned - - it is read only"

in the line where I want to give back the attributes ( properties[propname] in your method).

How doyouget the parameter for your method PreFilterProperties and how do you connect the new attributes with the instance of your control?

Many thanks in advanvce

Manfred

Manfred9091  Monday, February 09, 2009 7:46 PM

You can use google to search for other answers

Custom Search

More Threads

• VS2005 IDE changes to component model breaks my code.
• "ICO.ICO" - Icon Help
• GDI+ graphics and viewport
• Disabling the Forms Designer in VS2005
• Collapsible Listview control
• DataGridView - Saving data
• Customize Progress Bar
• How can I make my user control take my other controlslike a panel
• UPLOADING A PICTURE
• Error in VB project