|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
PropertyGrid CollectionEditor questionI have a class that works great within the propertygrid with an exception of
one member. It is a collection of a simple class objects that I have. I have tried everything but cant get the class to display in the CollectionEditor. What does it take to get the class to display in the CollectionEditor? Thanks, Bill, I'm new to PropertyGrids and had a similar question several weeks ago.
Some folks helped me out. I learn best by example so I'm pasting some code as an example of a class I have that is displayed in a propertygrid in my application. The second class, "FilePropertiesConverter" is the key. Hope this helps. using System; using System.IO; using System.Collections; using System.Collections.Specialized; using System.Windows.Forms; using System.ComponentModel; using System.ComponentModel.Design; using System.Drawing.Design; namespace CheckIt { /// <summary> /// This class defines the properties looked for when inspecting files. /// </summary> [TypeConverterAttribute(typeof(FilePropertiesConverter)), DescriptionAttribute("Define the File Test Properties"), Serializable()] public class FileProperties { /// <summary> /// Default constructor /// </summary> public FileProperties() { } /// <summary> /// The path to the file being inspected. /// </summary> private string filepath = ""; /// <summary> /// Get or set the path to the file that is being inspected. /// </summary> [Editor(typeof(System.Windows.Forms.Design.FileNameEditor), typeof(System.Drawing.Design.UITypeEditor)), Description("Identifies the path searched when looking for a file."), Category(TestCaseStrings.strTestComponent),DefaultValueAttribute("")] public string FilePath { get { return filepath; } set { filepath = value; if (filepath != null) FileSize = GetFileSize(); } } /// <summary> /// Filesize of the file to be inspected. /// </summary> private long filesize = 0; /// <summary> /// Get or set the filesize of the file. /// </summary> [Description("The value represents the file's size, in bytes, that is expected during the test."), Category(TestCaseStrings.strTestComponent), DefaultValueAttribute(0)] public long FileSize { get { return filesize; } set { filesize = value; } } /// <summary> /// Check the file size during the test. /// </summary> private bool checkFileSize = false; /// <summary> /// If true, the file size is checked during the test, else false.. /// </summary> [Description("Determines if the file's size should be checked during the test."), Category(TestCaseStrings.strTestComponent), DefaultValueAttribute(false)] public bool CheckFileSize { get { return this.checkFileSize; } set { this.checkFileSize = value; } } /// <summary> /// Gets the file size of the selected file. /// </summary> /// <returns>long representing the filesize in bytes.</returns> [Browsable(false)] private long GetFileSize() { if (File.Exists(filepath) == true) { FileInfo fileinfo = new FileInfo(filepath); return fileinfo.Length; } return 0; } } /// <summary> /// This class is the converter for the FileProperties class. /// </summary> public class FilePropertiesConverter : ExpandableObjectConverter { /// <summary> /// The destination class uses this filetype so return it. /// </summary> /// <param name="context"></param> /// <param name="destinationType"></param> /// <returns></returns> public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) { if (destinationType == typeof(FileProperties)) return true; return base.CanConvertTo (context, destinationType); } /// <summary> /// Override teh ConvertTo class to check for string types for the FileProperties class. /// </summary> /// <param name="context"></param> /// <param name="culture"></param> /// <param name="value"></param> /// <param name="destinationType"></param> /// <returns></returns> public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) { if (destinationType == typeof(System.String) && value is FileProperties) { FileProperties fp = (FileProperties)value; return "FilePath:" + fp.FilePath + ", FileSize: " + fp.FileSize + "CheckFileSize: " + fp.CheckFileSize; } return base.ConvertTo (context, culture, value, destinationType); } } } Show quote "Bill Gauvey" wrote: > I have a class that works great within the propertygrid with an exception of > one member. It is a collection of a simple class objects that I have. I have > tried everything but cant get the class to display in the CollectionEditor. > What does it take to get the class to display in the CollectionEditor? > > > Thanks, Actually I had to create a custom collection class. It seems that an
ArrayList will not work. TypeConverter will serialize the class and that is fine. My issue was that in the collection editor, none of the class members would appear even with the Type Converter in place. I got it resolved but thanks for the reply, I am sure it will be helpful to someone. Show quote "Steve Teeples" wrote: > Bill, I'm new to PropertyGrids and had a similar question several weeks ago. > Some folks helped me out. I learn best by example so I'm pasting some code > as an example of a class I have that is displayed in a propertygrid in my > application. The second class, "FilePropertiesConverter" is the key. Hope > this helps. > > using System; > using System.IO; > using System.Collections; > using System.Collections.Specialized; > using System.Windows.Forms; > using System.ComponentModel; > using System.ComponentModel.Design; > using System.Drawing.Design; > > namespace CheckIt > { > /// <summary> > /// This class defines the properties looked for when inspecting files. > /// </summary> > [TypeConverterAttribute(typeof(FilePropertiesConverter)), > DescriptionAttribute("Define the File Test Properties"), > Serializable()] > public class FileProperties > { > /// <summary> > /// Default constructor > /// </summary> > public FileProperties() > { > } > > /// <summary> > /// The path to the file being inspected. > /// </summary> > private string filepath = ""; > > /// <summary> > /// Get or set the path to the file that is being inspected. > /// </summary> > [Editor(typeof(System.Windows.Forms.Design.FileNameEditor), > typeof(System.Drawing.Design.UITypeEditor)), > Description("Identifies the path searched when looking for a file."), > Category(TestCaseStrings.strTestComponent),DefaultValueAttribute("")] > public string FilePath > { > get { return filepath; } > set > { > filepath = value; > if (filepath != null) > FileSize = GetFileSize(); > } > } > > /// <summary> > /// Filesize of the file to be inspected. > /// </summary> > private long filesize = 0; > > /// <summary> > /// Get or set the filesize of the file. > /// </summary> > [Description("The value represents the file's size, in bytes, that is > expected during the test."), > Category(TestCaseStrings.strTestComponent), DefaultValueAttribute(0)] > public long FileSize > { > get { return filesize; } > set { filesize = value; } > } > > /// <summary> > /// Check the file size during the test. > /// </summary> > private bool checkFileSize = false; > > /// <summary> > /// If true, the file size is checked during the test, else false.. > /// </summary> > [Description("Determines if the file's size should be checked during the > test."), > Category(TestCaseStrings.strTestComponent), DefaultValueAttribute(false)] > public bool CheckFileSize > { > get { return this.checkFileSize; } > set { this.checkFileSize = value; } > } > > /// <summary> > /// Gets the file size of the selected file. > /// </summary> > /// <returns>long representing the filesize in bytes.</returns> > [Browsable(false)] > private long GetFileSize() > { > if (File.Exists(filepath) == true) > { > FileInfo fileinfo = new FileInfo(filepath); > return fileinfo.Length; > } > return 0; > } > } > > /// <summary> > /// This class is the converter for the FileProperties class. > /// </summary> > public class FilePropertiesConverter : ExpandableObjectConverter > { > /// <summary> > /// The destination class uses this filetype so return it. > /// </summary> > /// <param name="context"></param> > /// <param name="destinationType"></param> > /// <returns></returns> > public override bool CanConvertTo(ITypeDescriptorContext context, Type > destinationType) > { > if (destinationType == typeof(FileProperties)) > return true; > > return base.CanConvertTo (context, destinationType); > } > > /// <summary> > /// Override teh ConvertTo class to check for string types for the > FileProperties class. > /// </summary> > /// <param name="context"></param> > /// <param name="culture"></param> > /// <param name="value"></param> > /// <param name="destinationType"></param> > /// <returns></returns> > public override object ConvertTo(ITypeDescriptorContext context, > System.Globalization.CultureInfo culture, object value, Type destinationType) > { > if (destinationType == typeof(System.String) && value is FileProperties) > { > FileProperties fp = (FileProperties)value; > return "FilePath:" + fp.FilePath + > ", FileSize: " + fp.FileSize + > "CheckFileSize: " + fp.CheckFileSize; > } > return base.ConvertTo (context, culture, value, destinationType); > } > } > } > > > "Bill Gauvey" wrote: > > > I have a class that works great within the propertygrid with an exception of > > one member. It is a collection of a simple class objects that I have. I have > > tried everything but cant get the class to display in the CollectionEditor. > > What does it take to get the class to display in the CollectionEditor? > > > > > > Thanks, |
|||||||||||||||||||||||