Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / Design Time / June 2005

Tip: Looking for answers? Try searching our database.

Yet Another Design Time Collection Query...

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
news.microsoft.com - 23 May 2005 05:38 GMT
Hi,
I am a new .NET programmer starting with the Visual C# 2005 Express Beta 2,
trying to replicate a VCL component that I use.  I am trying to handle a
collection of classes within the component, this class has its own
properties that are editable by the standard collection editor.  To break
down the problem as much as possible, I have the following code:

namespace mycomps {

public partial class testcomp : UserControl
{
   public class itemcollection : List<item>
   {
       public itemcollection() : base() { }
       public itemcollection(IEnumerable<item> collection) :
base(collection) { }
   }
   private itemcollection FItems;
   public testcomp()
   {
       FItems = new itemcollection();
       InitializeComponent();
   }
   [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
   public itemcollection Items { get { return FItems; } }
}

[TypeConverter(typeof(item.converter))]
public class item
{
   private string FCaption;
   public item()
   {
   }
   public item(string caption)
   {
       FCaption = caption;
   }
   public class converter : TypeConverter
   {
       public override bool CanConvertTo(ITypeDescriptorContext context,
Type destinationType)
       {
           if (destinationType ==
typeof(System.ComponentModel.Design.Serialization.InstanceDescriptor))
               return true;
           return base.CanConvertTo(context, destinationType);
       }
       public override object ConvertTo(ITypeDescriptorContext context,
System.Globalization.CultureInfo culture, object value, Type
destinationType)
       {
           if (destinationType ==
typeof(System.ComponentModel.Design.Serialization.InstanceDescriptor))
           {
               item value_item = (item)value;
               System.Reflection.ConstructorInfo ctor =
typeof(item).GetConstructor(new Type[] { typeof(string) });
               return new
System.ComponentModel.Design.Serialization.InstanceDescriptor(ctor, new
object[] { value_item.FCaption });
           }
           return base.ConvertTo(context, culture, value, destinationType);
       }
   }
   public string Caption { get { return FCaption; } set { FCaption =
value; } }
}

} // namespace mycomps

When I place this component on a form, I can edit the Items property ok,
adding new items and changing the Caption property.  But when I hit ok and
try to run it or look at the designer code I get the following in a message
box:

Code generation for property 'Items' failed. Error was: 'Unable to case
object of type 'mycomps.item' to type 'mycomps.item'.'

Thanks for any help that anyone can give,

Steve
Phil Wright - 04 Jun 2005 21:09 GMT
Try making your item class derive from Component instead of being a
standalone root class. I have had code generation problems with collections
in the past and making sure your item class comes from the standard
Component base seems to solve most issues!

Phil Wright
Follow the microISV story at...
http://componentfactory.blogspot.com

> Hi,
> I am a new .NET programmer starting with the Visual C# 2005 Express Beta
[quoted text clipped - 80 lines]
>
> Steve

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.