MSDN > Home page del forum > Windows Forms Designer > Adding Attributes to Properties At Runtime in C#...
Formula una domandaFormula una domanda
 

Con rispostaAdding Attributes to Properties At Runtime in C#...

  • venerdì 25 maggio 2007 13.22Rajesh_2959 Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     

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

Risposte

  • venerdì 25 maggio 2007 18.55Malacki Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     Con risposta
    Typo: change prop = TypeDescriptor.CreateProperty(this.GetType(), propname, prop.PropertyType, attrs);   to prop = TypeDescriptor.CreateProperty(control.GetType(), propname, prop.PropertyType, attrs);

    Marc.

Tutte le risposte

  • venerdì 25 maggio 2007 18.55Malacki Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     Con risposta
    Typo: change prop = TypeDescriptor.CreateProperty(this.GetType(), propname, prop.PropertyType, attrs);   to prop = TypeDescriptor.CreateProperty(control.GetType(), propname, prop.PropertyType, attrs);

    Marc.
  • lunedì 28 maggio 2007 13.32Rajesh_2959 Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     

    thanks a  lot Malacki !!

    It has worked :-)

     

    thanks for your time.......

     

    rajesh

  • lunedì 9 febbraio 2009 19.46Manfred9091 Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     

    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