Adding Attributes to Properties At Runtime in C#...<p>Hi !</p> <p> </p> <p>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. </p> <p>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....</p> <p> </p> <p>I have an issue in using Attributes for the Properites of the different controls. <br>I am using a Custom ControlDesigner to filter out certain properties and adding some properties.</p> <p>while filtering out properties,i use the BrowsableAttribute class as:</p> <p>protected override void </p> <p>                PreFilterProperties(System.Collections.IDictionary properties)<br>{<br>          <br>        base.PreFilterProperties(properties);<br> string[] propertiesNotToShow = { <br>                &quot;AutoCenter&quot;, &quot;AutoFrameRate&quot;, &quot;AutoScaleEnabled&quot;, &quot;AutoScaleStyle&quot;, &quot;BackGroundPicture&quot;, &quot;BorderStyle&quot;, &quot;CachedDrawing&quot;, &quot;ComponentHandle&quot;<br>  }  //i have some 60 odd properties<br>        </p> <p> foreach (string propname in propertiesNotToShow)<br>            {<br>                prop = (PropertyDescriptor)properties[propname];<br>                if (prop != null)<br>                {<br>                    AttributeCollection runtimeAttributes = prop.Attributes;<br>                    // make a copy of the original attributes <br>                    // but make room for one extra attribute<br>                    Attribute[] attrs = new Attribute[runtimeAttributes.Count + 1];<br>                    runtimeAttributes.CopyTo(attrs, 0);<br>                    attrs[runtimeAttributes.Count] = new BrowsableAttribute(false); </p> <p>                  // makes this Property hidden in PropertyGrid<br>                    prop = TypeDescriptor.CreateProperty(this.GetType(), propname, prop.PropertyType, attrs);<br>                    properties[propname] = prop;<br>                }<br>            }<br>}</p> <p>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 &quot;Object reference not set to an instance of an object&quot;.</p> <p>Can anyone please explain why this happens with these set of attributes and for BrowsableAttribute it works fine....</p>© 2009 Microsoft Corporation. All rights reserved.Mon, 09 Feb 2009 19:46:05 Z4fb1b301-618d-4b5e-8b89-e0595c005b16http://social.msdn.microsoft.com/Forums/en-US/winformsdesigner/thread/4fb1b301-618d-4b5e-8b89-e0595c005b16#4fb1b301-618d-4b5e-8b89-e0595c005b16http://social.msdn.microsoft.com/Forums/en-US/winformsdesigner/thread/4fb1b301-618d-4b5e-8b89-e0595c005b16#4fb1b301-618d-4b5e-8b89-e0595c005b16Rajesh_2959http://social.msdn.microsoft.com/Profile/en-US/?user=Rajesh_2959Adding Attributes to Properties At Runtime in C#...<p>Hi !</p> <p> </p> <p>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. </p> <p>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....</p> <p> </p> <p>I have an issue in using Attributes for the Properites of the different controls. <br>I am using a Custom ControlDesigner to filter out certain properties and adding some properties.</p> <p>while filtering out properties,i use the BrowsableAttribute class as:</p> <p>protected override void </p> <p>                PreFilterProperties(System.Collections.IDictionary properties)<br>{<br>          <br>        base.PreFilterProperties(properties);<br> string[] propertiesNotToShow = { <br>                &quot;AutoCenter&quot;, &quot;AutoFrameRate&quot;, &quot;AutoScaleEnabled&quot;, &quot;AutoScaleStyle&quot;, &quot;BackGroundPicture&quot;, &quot;BorderStyle&quot;, &quot;CachedDrawing&quot;, &quot;ComponentHandle&quot;<br>  }  //i have some 60 odd properties<br>        </p> <p> foreach (string propname in propertiesNotToShow)<br>            {<br>                prop = (PropertyDescriptor)properties[propname];<br>                if (prop != null)<br>                {<br>                    AttributeCollection runtimeAttributes = prop.Attributes;<br>                    // make a copy of the original attributes <br>                    // but make room for one extra attribute<br>                    Attribute[] attrs = new Attribute[runtimeAttributes.Count + 1];<br>                    runtimeAttributes.CopyTo(attrs, 0);<br>                    attrs[runtimeAttributes.Count] = new BrowsableAttribute(false); </p> <p>                  // makes this Property hidden in PropertyGrid<br>                    prop = TypeDescriptor.CreateProperty(this.GetType(), propname, prop.PropertyType, attrs);<br>                    properties[propname] = prop;<br>                }<br>            }<br>}</p> <p>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 &quot;Object reference not set to an instance of an object&quot;.</p> <p>Can anyone please explain why this happens with these set of attributes and for BrowsableAttribute it works fine....</p>Fri, 25 May 2007 13:22:28 Z2007-05-29T11:10:05Zhttp://social.msdn.microsoft.com/Forums/en-US/winformsdesigner/thread/4fb1b301-618d-4b5e-8b89-e0595c005b16#5293c7e5-09ea-4820-8892-46f85a03b714http://social.msdn.microsoft.com/Forums/en-US/winformsdesigner/thread/4fb1b301-618d-4b5e-8b89-e0595c005b16#5293c7e5-09ea-4820-8892-46f85a03b714Malackihttp://social.msdn.microsoft.com/Profile/en-US/?user=MalackiAdding Attributes to Properties At Runtime in C#...Typo: change <em>prop = TypeDescriptor.CreateProperty(<strong>this.GetType()</strong>, propname, prop.PropertyType, attrs);</em><strong> </strong>  to <em>prop = TypeDescriptor.CreateProperty(c<strong>ontrol.GetType()</strong>, propname, prop.PropertyType, attrs);</em><br><br>Marc.Fri, 25 May 2007 18:55:46 Z2007-05-29T11:10:05Zhttp://social.msdn.microsoft.com/Forums/en-US/winformsdesigner/thread/4fb1b301-618d-4b5e-8b89-e0595c005b16#ff8cb8b8-03d5-4a43-b1ff-e55a1b81f77ahttp://social.msdn.microsoft.com/Forums/en-US/winformsdesigner/thread/4fb1b301-618d-4b5e-8b89-e0595c005b16#ff8cb8b8-03d5-4a43-b1ff-e55a1b81f77aRajesh_2959http://social.msdn.microsoft.com/Profile/en-US/?user=Rajesh_2959Adding Attributes to Properties At Runtime in C#...<p>thanks a  lot Malacki !!</p> <p>It has worked :-)</p> <p> </p> <p>thanks for your time.......</p> <p> </p> <p>rajesh</p>Mon, 28 May 2007 13:32:46 Z2007-05-28T13:32:46Zhttp://social.msdn.microsoft.com/Forums/en-US/winformsdesigner/thread/4fb1b301-618d-4b5e-8b89-e0595c005b16#8f712f44-d26b-49c5-ac73-53f200c205adhttp://social.msdn.microsoft.com/Forums/en-US/winformsdesigner/thread/4fb1b301-618d-4b5e-8b89-e0595c005b16#8f712f44-d26b-49c5-ac73-53f200c205adManfred9091http://social.msdn.microsoft.com/Profile/en-US/?user=Manfred9091Adding Attributes to Properties At Runtime in C#...<p> Hi rajesh,</p> <p>I've to fix the same problem as you did in 2007. </p> <p>When I try to use the same solution as you I get the error message </p> <p>&quot;property or indexer 'System.ComponentModel.PropertyDescriptorCollection.this[string] cannot be assigned - - it is read only&quot;<br><br>in the line where I want to give back the attributes ( properties[propname] in your method).</p> <p>How do you get the parameter for your method PreFilterProperties and how do you connect the new attributes with the instance of your control?</p> <p>Many thanks in advanvce</p> <p>Manfred</p>Mon, 09 Feb 2009 19:46:01 Z2009-02-09T19:46:01Z