System.Windows.Forms.ImageListUtils.GetImageListProperty C# (CSharp) Method

GetImageListProperty() public static method

public static GetImageListProperty ( PropertyDescriptor currentComponent, object &instance ) : PropertyDescriptor
currentComponent System.ComponentModel.PropertyDescriptor
instance object
return System.ComponentModel.PropertyDescriptor
        public static PropertyDescriptor GetImageListProperty(PropertyDescriptor currentComponent, ref object instance) {
            if(instance is object[]) //multiple selection is not supported by this class
                return null;
            
            PropertyDescriptor imageListProp = null;
            object parentInstance = instance;
            
            RelatedImageListAttribute relILAttr = currentComponent.Attributes[typeof(RelatedImageListAttribute)] as RelatedImageListAttribute;
            if (relILAttr != null) 
            {
                string[] pathInfo = relILAttr.RelatedImageList.Split('.');
                for(int i=0;i<pathInfo.Length;i++) {
                    if(parentInstance == null) {
                        Debug.Fail("A property specified in the path is null or not yet instanciated at this time");
                        break;
                    }
                    PropertyDescriptor prop = TypeDescriptor.GetProperties(parentInstance)[pathInfo[i]];
                    if(prop == null) {
                        Debug.Fail("The path specified to the property is wrong");
                        break;
                    }
                    if(i==pathInfo.Length-1) {
                        // we're on the last one, look if that's our guy
                        if (typeof(ImageList).IsAssignableFrom(prop.PropertyType)) {
                            instance = parentInstance;
                            imageListProp = prop;
                            break;
                        }
                    } else {
                        parentInstance = prop.GetValue(parentInstance);                                    
                    }
                }
            }
    
            return imageListProp;
        }
    }

Usage Example

Example #1
0
 public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
 {
     if ((context != null) && (context.Instance != null))
     {
         object             instance          = context.Instance;
         PropertyDescriptor imageListProperty = ImageListUtils.GetImageListProperty(context.PropertyDescriptor, ref instance);
         while ((instance != null) && (imageListProperty == null))
         {
             PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(instance);
             foreach (PropertyDescriptor descriptor2 in properties)
             {
                 if (typeof(ImageList).IsAssignableFrom(descriptor2.PropertyType))
                 {
                     imageListProperty = descriptor2;
                     break;
                 }
             }
             if (imageListProperty == null)
             {
                 PropertyDescriptor descriptor3 = properties[this.ParentImageListProperty];
                 if (descriptor3 != null)
                 {
                     instance = descriptor3.GetValue(instance);
                     continue;
                 }
                 instance = null;
             }
         }
         if (imageListProperty != null)
         {
             ImageList list = (ImageList)imageListProperty.GetValue(instance);
             if (list != null)
             {
                 object[] objArray;
                 int      count = list.Images.Count;
                 if (this.IncludeNoneAsStandardValue)
                 {
                     objArray        = new object[count + 1];
                     objArray[count] = -1;
                 }
                 else
                 {
                     objArray = new object[count];
                 }
                 for (int i = 0; i < count; i++)
                 {
                     objArray[i] = i;
                 }
                 return(new TypeConverter.StandardValuesCollection(objArray));
             }
         }
     }
     if (this.IncludeNoneAsStandardValue)
     {
         return(new TypeConverter.StandardValuesCollection(new object[] { -1 }));
     }
     return(new TypeConverter.StandardValuesCollection(new object[0]));
 }
All Usage Examples Of System.Windows.Forms.ImageListUtils::GetImageListProperty
ImageListUtils