Posted By: ricodued | Jan 18th, 2006 @ 1:30 PM
page 1 of 1
Comments: 6 | Views: 3709
I've been writing a set of UI controls for use in my upcoming projects, mostly replicating the Visual Studio 2005 look and feel. The rendering is pretty much pixel-perfect, but when using the design-time capabilities of one of my controls (the sidebar), I get an issue when building.

In the .design.cs file of the form, where it sets the properties and whatnot of the SideBar, it puts this line of code:

...
this.sideBar1.Name = "sideBar1";

((Trowbridge.UI.SideBar.SidePanelCollection)(new Trowbridge.UI.SideBar.SidePanel())).Add(this.sidePanel1);

this.sideBar1.SelectedPanel = null;
...

The above code sets off the error at build: Cannot convert type 'Trowbridge.UI.SideBar.SidePanel' to 'Trowbridge.UI.SideBar.SidePanelCollection' (the line between the Name and SelectedPanel sets). Design-time functionality remains intact, the designer is not killed, it just won't build. I have a feeling it has to do with the casts...

The code above is set by the following designer code:

internal class SidePanelCollectionConverter : TypeConverter
{
   public override bool CanConvertTo(ITypeDescriptorContext context, Type destType)
   {
       if (destType == typeof(InstanceDescriptor))
           return true;

       return base.CanConvertTo(context, destType);
   }

   public override object ConvertTo(
                ITypeDescriptorContext context,
                System.Globalization.CultureInfo culture,
                object value, Type destType)
   {
       if (destType == typeof(InstanceDescriptor))
       {
           System.Reflection.ConstructorInfo ci = typeof(SidePanel).GetConstructor(System.Type.EmptyTypes);

           return new InstanceDescriptor(ci, null, false);
       }

       return base.ConvertTo(context, culture, value, destType);
   }
}

// apoligies for the pink.
// colorized for easier reading

If you'll note the line giving issues, it's making some sort of very strange casts that make no sense whatsoever to me. It looks like it should be this.sideBar1.Add(this.sidePanel1) to me, rather than those set of weird casts.

I am completely stumped. Anyone have any clues?

-Eric

P.S. If anyone cares, this code was based on Tim Dawson's Creating Collection Controls with Rich Design Time Support article.
ricodued wrote:

(
   (Trowbridge.UI.SideBar.SidePanelCollection)
   (new Trowbridge.UI.SideBar.SidePanel())
)


This is the bit causing the error. You're creating a new SidePanel, then casting it to a SidePanelCollection, which won't work because SidePanel doesn't derive from SidePanelCollection.

EDIT: Just realised you meant the designer creates broken code. What do your constructors look like? Also what does the code for adding a Panel look like (the bit that calls CreateComponent)
ricodued wrote:

   public override object ConvertTo(
   ...
           System.Reflection.ConstructorInfo ci = typeof(SidePanel).GetConstructor(System.Type.EmptyTypes);
...


Shouldn't that say typeof(SidePanelCollection) there?
ricodued wrote:
gregoryw wrote:
ricodued wrote:
   public override object ConvertTo(
   ...
           System.Reflection.ConstructorInfo ci = typeof(SidePanel).GetConstructor(System.Type.EmptyTypes);
...


Shouldn't that say typeof(SidePanelCollection) there?


You'd totally be my hero, except it raises another problem. The designer doesn't add the line which adds all the SidePanels to its collection, now, after changing typeof(SidePanel) to typeof(SidePanelCollection).

So it just looks like this:

            this.sideBar1.Name = "sideBar1";
            this.sideBar1.SelectedPanel = this.sidePanel1;

I didn't think it'd be the problem, as in the other controls based on this code (the tab control), it's set as the TabContainerPage, not TabContainerCollection.

Strange.


Have you applied the following attribute to the collection property on your control?

DesignerSerializationVisibility(DesignerSerializationVisibility.Content)
page 1 of 1
Comments: 6 | Views: 3709
Microsoft Communities