Martijn
Martijn

Reputation: 24789

C# Winforms Visual Studio designer cannot find custom type

I have a custom control with a generic list of custom types. This list is defined public:

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content), Editor(typeof(ButtonPanelXEditor), typeof(UITypeEditor))]
public List<CompactButton> CompactButtons
{
    get { return _compactButtons; }
    set { _compactButtons = value; }
}

When I add this control to my form and build my project I get this error:

Error 1 Could not find a type for a name. The type name was 'ButtonPanelX.CompactButton, ButtonPanelX, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. Line 127, position 5. D:\Projecten\ButtonPanelX\ButtonPanelX\Form1.resx 127 5 ButtonPanelX

When I use strings instead of custom objects, the desginer does save my list. CompactButton has the attribute [Serializable] and derives from ISerializable

What can I do to fix this?

Edit:

public class ButtonPanelXEditor : UITypeEditor
{
    public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
    {
        if (context != null && context.Instance != null)
            // We will use a window for property editing. 
            return UITypeEditorEditStyle.Modal;

        return base.GetEditStyle(context);
    }

    public override object EditValue(System.ComponentModel.ITypeDescriptorContext context,
        IServiceProvider provider, object value)
    {

        context.OnComponentChanging();

        ButtonPanel b = context.Instance as ButtonPanel;

        FooBar form = new FooBar();
        form.Buttons = b.CompactButtons;

        form.ShowDialog();

        b.CompactButtons = form.Buttons;

        b.DrawButtons();

        context.OnComponentChanged();

        return form.Buttons;
    }
}

EDIT 2:

[Serializable]
public partial class ButtonPanel : UserControl
{
    private ArrayList _compactButtons;

    public ButtonPanel()
    {
        InitializeComponent();

        _compactButtons = new ArrayList();

        AddButtons();

        this.Load += new EventHandler(ButtonPanel_Load);

    }

    void ButtonPanel_Load(object sender, EventArgs e)
    {
        DrawButtons();
    }


    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), Editor(typeof(ButtonPanelXEditor), typeof(UITypeEditor))]
    public ArrayList CompactButtons
    {
        get { return _compactButtons; }
    }

    public void DrawButtons()
    {
        baseButton1.Visible = ((CompactButton)_compactButtons[0]).Visible;
        baseButton2.Visible = ((CompactButton)_compactButtons[1]).Visible;
    }

    private void AddButtons()
    {
        /* Buttons baseButton1 and baseButton2 are created by the designer */

        CompactButton c = new CompactButton();
        c.Enabled = baseButton1.Enabled;
        c.Visible = baseButton1.Visible;
        c.Name = baseButton1.Name;

        CompactButton c2 = new CompactButton();
        c2.Enabled = baseButton2.Enabled;
        c2.Visible = baseButton2.Visible;
        c2.Name = baseButton2.Name;

        _compactButtons.Add(c);
        _compactButtons.Add(c2);
    }
}

Upvotes: 0

Views: 2250

Answers (1)

Patko
Patko

Reputation: 4423

Instead of serializing your buttons to the resource file, you could try to serialize them to the code behind. For this you need to implement a custom TypeDescriptor for your CompactButton type and there handle convertion to an InstanceDescriptor. Look at How to Implement a TypeConverter. Example:

[TypeConverter(typeof(CompactButtonTypeConverter))]
public class CompactButton: ... {
  ...
}

public class CompactButtonTypeConverter: TypeConverter {

  public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) {
    if (destinationType == typeof(InstanceDescriptor)) 
       return true;
    return base.CanConvertTo(context, destinationType);
  }

  public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) {
    if (destinationType == typeof(InstanceDescriptor) && value is CompactButton) {
      // This assumes you have a public default constructor on your type.
      ConstructorInfo ctor = typeof(CompactButton).GetConstructor();
      if (ctor != null) 
         return new InstanceDescriptor(ctor, new object[0], false);
    }
    return base.ConvertTo(context, culture, value, destinationType);      
  }

}

For more information also see the InstanceDescriptor class.

UPDATE: As for your UITypeEditor and the CompactButtons property, you do not need a setter. Change your CompactButtons property as follows:

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content), Editor(typeof(ButtonPanelXEditor), typeof(UITypeEditor))]
public List<CompactButton> CompactButtons
{
    get { return _compactButtons; } // _compactButtons must of course be initialized.
}

Then you could implement the EditValue method of UITypeEditor like so:

public override object EditValue(System.ComponentModel.ITypeDescriptorContext context,
  IServiceProvider provider, object value) {
  if (context == null || provider == null)
    return null;

  var b = context.Instance as ButtonPanel;
  if (b == null)
    return value;

  var editorService = (IWindowsFormsEditorService)
    provider.GetService(typeof(IWindowsFormsEditorService));
  if (editorService == null)
    return null;

  // This constructor should copy the buttons in its own list.
  using (var form = new FooBar(b.CompactButtons)) {
    if (editorService.ShowDialog(form) == DialogResult.OK && context.OnComponentChanging()) {
      b.CompactButtons.Clear();
      b.CompactButtons.AddRange(form.Buttons);
      context.OnComponentChanged();
    }
  }
  return value;
}

If your editor form is not very specialized you could maybe try out the CollectionEditor.

Upvotes: 2

Related Questions