MSDN > 論壇首頁 > Windows Forms Designer > Adding Attributes to Properties At Runtime in C#...
發問發問
 

已答覆Adding Attributes to Properties At Runtime in C#...

  • 2007年5月25日 下午 01:22Rajesh_2959 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     

    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....

解答

  • 2007年5月25日 下午 06:55Malacki 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     已答覆
    Typo: change prop = TypeDescriptor.CreateProperty(this.GetType(), propname, prop.PropertyType, attrs);   to prop = TypeDescriptor.CreateProperty(control.GetType(), propname, prop.PropertyType, attrs);

    Marc.

所有回覆

  • 2007年5月25日 下午 06:55Malacki 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     已答覆
    Typo: change prop = TypeDescriptor.CreateProperty(this.GetType(), propname, prop.PropertyType, attrs);   to prop = TypeDescriptor.CreateProperty(control.GetType(), propname, prop.PropertyType, attrs);

    Marc.
  • 2007年5月28日 下午 01:32Rajesh_2959 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     

    thanks a  lot Malacki !!

    It has worked :-)

     

    thanks for your time.......

     

    rajesh

  • 2009年2月9日 下午 07:46Manfred9091 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     

    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 do you get 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