Click here to Skip to main content
15,921,660 members
Home / Discussions / C#
   

C#

 
GeneralRe: Do you think this is stupid ? Pin
Colin Angus Mackay7-May-05 3:37
Colin Angus Mackay7-May-05 3:37 
GeneralRe: Do you think this is stupid ? Pin
Sharpoverride7-May-05 3:42
Sharpoverride7-May-05 3:42 
AnswerRe: Do you think this is stupid ? Pin
Daniel Turini7-May-05 3:41
Daniel Turini7-May-05 3:41 
GeneralRe: Do you think this is stupid ? Pin
Sharpoverride7-May-05 3:44
Sharpoverride7-May-05 3:44 
GeneralCharacters in hexadecimal Pin
Shashidharreddy7-May-05 0:23
Shashidharreddy7-May-05 0:23 
GeneralRe: Characters in hexadecimal Pin
S. Senthil Kumar7-May-05 1:42
S. Senthil Kumar7-May-05 1:42 
GeneraloleDb Insert problem Pin
gmeii6-May-05 22:47
gmeii6-May-05 22:47 
GeneralRe: oleDb Insert problem Pin
Ken Haley7-May-05 3:37
Ken Haley7-May-05 3:37 
GeneralRepaint form using Invalidate() Pin
gmeii6-May-05 22:32
gmeii6-May-05 22:32 
GeneralRe: Repaint form using Invalidate() Pin
Sharpoverride7-May-05 2:49
Sharpoverride7-May-05 2:49 
GeneralFlash Splash Screen Pin
gmeii6-May-05 22:08
gmeii6-May-05 22:08 
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 

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.