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);
}
}
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
TypeDescriptorfor yourCompactButtontype and there handle convertion to anInstanceDescriptor. Look at How to Implement a TypeConverter. Example:For more information also see the InstanceDescriptor class.
UPDATE: As for your
UITypeEditorand the CompactButtons property, you do not need a setter. Change your CompactButtons property as follows:Then you could implement the EditValue method of
UITypeEditorlike so:If your editor form is not very specialized you could maybe try out the CollectionEditor.