Click here to Skip to main content
15,890,438 members
Home / Discussions / C#
   

C#

 
GeneralRe: Flash Splash Screen Pin
Sharpoverride7-May-05 2:45
Sharpoverride7-May-05 2:45 
GeneralOffice automation from C# Pin
Member 18788066-May-05 21:06
Member 18788066-May-05 21:06 
GeneralRe: Office automation from C# Pin
EEmad4-Jun-05 19:17
sussEEmad4-Jun-05 19:17 
Questionbones in meshes and directx? Pin
taha mohamed6-May-05 19:18
taha mohamed6-May-05 19:18 
GeneralCustomizing .NET Collection Editor Pin
heavenamour6-May-05 18:50
heavenamour6-May-05 18:50 
GeneralRe: Customizing .NET Collection Editor Pin
spif20016-May-05 21:04
spif20016-May-05 21:04 
GeneralRe: Customizing .NET Collection Editor Pin
heavenamour7-May-05 0:08
heavenamour7-May-05 0:08 
GeneralRe: Customizing .NET Collection Editor Pin
StealthyMark7-May-05 12:15
StealthyMark7-May-05 12:15 
heavenamour wrote:
I Wrote a calendar class that have a collection of holidays as an ArrayList. This collection is a property of my custom control that I want Add some objects of Holiday class to it at design time.

You have to do three things:
1. Use a strongly typed Collection, derived from CollectionBase instead of an ArrayList or Array. This allows the CollectionEditor to work properly.

2. Create a custom TypeConverter for Holiday, which is capable of converting a Holiday instance to an InstanceDescriptor. This allows the code generator to generate code to instantiate Holiday objects.

3. Decorate the collection property of your custom control with the DesignerSerializationVisibility attribute. This tells the code generator to serialize the contents of your collection, rather than the collection itself.

To illustrate these points:
using System;
using System.Collections;
using System.ComponentModel;
using System.Globalization;
using System.ComponentModel.Design.Serialization;
using System.Reflection;
 
namespace Holidays
{
    public class YourControl
    {
        private readonly HolidayCollection holidays = new HolidayCollection();
 
        // This attribute tells the code generator to serialize the contained items
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public HolidayCollection Holidays
        {
            get { return holidays; }
        }
    }
 
 
    // The strongly typed collection
    // Never expose an ArrayList or an Array as a public property!
    public class HolidayCollection : CollectionBase
    {
        public int Add(Holiday item)
        { return List.Add(item); }
 
        public Holiday this[int index]
        {
            get { return (Holiday)List[index]; }
            set { List[index] = value; }
        }
    }
 
 
    // assign the converter
    [TypeConverter(typeof(HolidayConverter))]
    public class Holiday
    {
        public Holiday()
        {
            this.month = -1;
            this.day = -1;
            this.reason = null;
        }
        
        public Holiday(int month, int day, string reason)
        {
            // add validation logic here
            this.month = month;
            this.day = day;
            this.reason = reason;
        }
 
        private int month;
        private int day;
        private string reason;
 
        public int Month
        {
            get { return month; }
            set { month = value; }
        }
 
        public int Day
        {
            get { return day; }
            set { day = value; }
        }
 
        public string Reason
        {
            get { return reason; }
            set { reason = value; }
        }
    }
 
 
    // The converter for Holiday. It's main purpose is to support code generation
    // It also makes the object "expandable", like e.g. Point or Size.
    internal class HolidayConverter : ExpandableObjectConverter
    {
        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            // An InstanceDescriptor is required for code generation
            if (destinationType == typeof(InstanceDescriptor))
                return true;
 
            // delegate other conversions
            return base.CanConvertTo(context, destinationType);
        }
 
 
        public override object ConvertTo(ITypeDescriptorContext context, 
            CultureInfo culture, object value, Type destinationType)
        {
            // cast value
            Holiday holiday = value as Holiday;
 
            // the InstanceDescriptor is used by the CodeGenerator to
            // get information about how to construct the value
            if (destinationType == typeof(InstanceDescriptor) && holiday != null)
            {
                // the arguments of the constructor (int month, int day, string reason)
                Type[] argumentTypes = new Type[] { typeof(int), typeof(int), typeof(string) };
 
                // get the constructor
                ConstructorInfo constructor = typeof(Holiday).GetConstructor(argumentTypes);
 
                // array with the actual constructor arguments
                object[] arguments = new object[] { holiday.Month, holiday.Day, holiday.Reason };
 
                if (constructor != null)
                    // return the instance descriptor
                    return new InstanceDescriptor(constructor, arguments, false);
            }
 
            // delegate other conversions
            return base.ConvertTo(context, culture, value, destinationType);
        }
    }
}

HTH, Mark

Edit: I forgot one more thing. The CollectionEditor doesn't like non-default constructors, in fact, it can't instantiate objects without default constructors. So...

4. Add a default constructor to Holiday. The CollectionEditor has no way of specifiying constructor arguments.
GeneralCustomizing .NET Collection Editor Pin
heavenamour6-May-05 18:49
heavenamour6-May-05 18:49 
Generaldisplaying and zooming in and out of a pdf Pin
brian556-May-05 11:58
brian556-May-05 11:58 
GeneralRe: displaying and zooming in and out of a pdf Pin
Yulianto.6-May-05 16:23
Yulianto.6-May-05 16:23 
GeneralRe: displaying and zooming in and out of a pdf Pin
brian556-May-05 16:56
brian556-May-05 16:56 
GeneralRe: displaying and zooming in and out of a pdf Pin
Yulianto.6-May-05 17:07
Yulianto.6-May-05 17:07 
GeneralRe: displaying and zooming in and out of a pdf Pin
Uwe Keim7-May-05 5:23
sitebuilderUwe Keim7-May-05 5:23 
GeneralProblem with redrawing text in a windows form Pin
Darktaz6-May-05 10:44
Darktaz6-May-05 10:44 
GeneralRe: Problem with redrawing text in a windows form Pin
MoustafaS6-May-05 13:09
MoustafaS6-May-05 13:09 
GeneralRe: Problem with redrawing text in a windows form Pin
Darktaz6-May-05 13:34
Darktaz6-May-05 13:34 
GeneralRe: Problem with redrawing text in a windows form Pin
MoustafaS6-May-05 14:15
MoustafaS6-May-05 14:15 
GeneralForm.Hide(), or Form.Close() not working Pin
Jeea6-May-05 10:01
Jeea6-May-05 10:01 
GeneralRe: Form.Hide(), or Form.Close() not working Pin
Dave Kreskowiak6-May-05 12:08
mveDave Kreskowiak6-May-05 12:08 
GeneralNewly created file stays locked Pin
dratcha6-May-05 9:46
dratcha6-May-05 9:46 
GeneralRe: Newly created file stays locked Pin
Colin Angus Mackay6-May-05 10:52
Colin Angus Mackay6-May-05 10:52 
GeneralRe: Newly created file stays locked Pin
dratcha6-May-05 11:18
dratcha6-May-05 11:18 
GeneralForm Show() method hangs, does not display controls Pin
methodincharge6-May-05 7:26
methodincharge6-May-05 7:26 
GeneralRe: Form Show() method hangs, does not display controls Pin
S. Senthil Kumar6-May-05 7:59
S. Senthil Kumar6-May-05 7:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.