/// <summary> /// Retrieves a collection containing a set of standard values /// for the data type this validator is designed for. This /// will return null if the data type does not support a /// standard set of values. /// </summary> public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext?context) { if (context is not null && context.Instance is not null) { object?instance = context.Instance; PropertyDescriptor?imageListProp = ImageListUtils.GetImageListProperty(context.PropertyDescriptor, ref instance); while (instance is not null && imageListProp is null) { PropertyDescriptorCollection props = TypeDescriptor.GetProperties(instance); foreach (PropertyDescriptor prop in props) { if (typeof(ImageList).IsAssignableFrom(prop.PropertyType)) { imageListProp = prop; break; } } if (imageListProp is null) { // We didn't find the image list in this component. See if the // component has a "parent" property. If so, walk the tree... PropertyDescriptor?parentProp = props[ParentImageListProperty]; if (parentProp is not null) { instance = parentProp.GetValue(instance); } else { // Stick a fork in us, we're done. instance = null; } } } if (imageListProp is not null) { ImageList?imageList = (ImageList?)imageListProp.GetValue(instance); if (imageList is not null) { // Create array to contain standard values int nImages = imageList.Images.Count + 2; object[] values = new object[nImages]; values[nImages - 2] = ImageList.Indexer.DefaultIndex; values[nImages - 1] = -2; // Fill in the array for (int i = 0; i < nImages - 2; i++) { values[i] = i; } return(new StandardValuesCollection(values)); } } } return(new StandardValuesCollection( new object[] { ImageList.Indexer.DefaultIndex, ImageList.Indexer.NoneIndex })); }
/// <devdoc> /// Retrieves a collection containing a set of standard values /// for the data type this validator is designed for. This /// will return null if the data type does not support a /// standard set of values. /// </devdoc> public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) { if (context != null && context.Instance != null) { object instance = context.Instance; PropertyDescriptor imageListProp = ImageListUtils.GetImageListProperty(context.PropertyDescriptor, ref instance); while (instance != null && imageListProp == null) { PropertyDescriptorCollection props = TypeDescriptor.GetProperties(instance); foreach (PropertyDescriptor prop in props) { if (typeof(ImageList).IsAssignableFrom(prop.PropertyType)) { imageListProp = prop; break; } } if (imageListProp == null) { // We didn't find the image list in this component. See if the // component has a "parent" property. If so, walk the tree... // PropertyDescriptor parentProp = props[ParentImageListProperty]; if (parentProp != null) { instance = parentProp.GetValue(instance); } else { // Stick a fork in us, we're done. // instance = null; } } } if (imageListProp != null) { ImageList imageList = (ImageList)imageListProp.GetValue(instance); if (imageList != null) { // Create array to contain standard values // object[] values; int nImages = imageList.Images.Count; if (IncludeNoneAsStandardValue) { values = new object[nImages + 1]; values[nImages] = -1; } else { values = new object[nImages]; } // Fill in the array // for (int i = 0; i < nImages; i++) { values[i] = i; } return(new StandardValuesCollection(values)); } } } if (IncludeNoneAsStandardValue) { return(new StandardValuesCollection(new object[] { -1 })); } else { return(new StandardValuesCollection(new object[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] = ""; } else { objArray = new object[count]; } StringCollection keys = list.Images.Keys; for (int i = 0; i < keys.Count; i++) { if ((keys[i] != null) && (keys[i].Length != 0)) { objArray[i] = keys[i]; } } return(new TypeConverter.StandardValuesCollection(objArray)); } } } if (this.IncludeNoneAsStandardValue) { return(new TypeConverter.StandardValuesCollection(new object[] { "" })); } return(new TypeConverter.StandardValuesCollection(new object[0])); }