1

I have implemented StringCollection editor in my custom control and below is the code :

[Description("extra free-form attributes on this thing.")]
[Editor(@"System.Windows.Forms.Design.StringCollectionEditor," +
    "System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
   typeof(System.Drawing.Design.UITypeEditor))]
public System.Collections.Specialized.StringCollection Items
{
   get
   {
      if (items == null)
         items = new System.Collections.Specialized.StringCollection();

      return  this.items;
   }
}

public System.Collections.Specialized.StringCollection items;

This works fine but every time i enter some value in the collection and re-open it.. values are lost i.e. it does not store the values.

Is there anything missing to store the value of user entered strings or do i need to implement custom StringCollection so that user entered string values persist in the String Editor.

I even refered to below given link.. but still issue exists : How can I use a WinForms PropertyGrid to edit a list of strings?

Community
  • 1
  • 1
user1291401
  • 264
  • 1
  • 6
  • 18

2 Answers2

1

Yes, you need to apply DesignerSerializationVisibility attribute to DesignerSerializationVisibility.Content. Without that all changes to the complex objects(other than primitives, strings, etc) will be lost.

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[Description("extra free-form attributes on this thing.")]
[Editor(@"System.Windows.Forms.Design.StringCollectionEditor," +
    "System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
   typeof(System.Drawing.Design.UITypeEditor))]
public System.Collections.Specialized.StringCollection Items
{
   get
   {
      if (items == null)
         items = new System.Collections.Specialized.StringCollection();

      return  this.items;
   }
}
Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
  • as suggested .. i tried the above given option.. but still issue exists. – user1291401 Aug 22 '14 at 11:05
  • @user1291401 Just tested, that works fine for me. Which version of VisualStudio you use? Try cleaning the solution and rebuild it. Then check, that may help... – Sriram Sakthivel Aug 22 '14 at 11:09
  • @ Sriram Sakthivel - VS 2012 and even tried cleaning, rebuilding the solution .. still issue exists. And even it is not getting reflected in the listbox also. – user1291401 Aug 22 '14 at 11:19
  • @user1291401 What do you mean by *and even it is not getting reflected in the listbox also*? What listbox? Post a complete sample which demonstrates the problem. Otherwise hard to help, because it works for me. – Sriram Sakthivel Aug 22 '14 at 11:23
  • i have implemented custom control.. similar to ListBox. Now i'm using the StringCollection Editor so that user can enter strings in editor and it should be stored/reflected in my custom control. But every time i click StringCollection Editor it is always empty even though last time i enter few string values in it. Hence i mentioned it -- it is not getting reflected in the listbox. – user1291401 Aug 22 '14 at 11:39
  • Where does your control inherits from? Do you mind positing the custom control code? Only relevant code to reproduce the problem? – Sriram Sakthivel Aug 22 '14 at 11:40
0

You might also try creating the list in your constructor. That along with the string collection editor and DesignerSerializationVisibility attributes works for me.

[Editor("System.Windows.Forms.Design.StringCollectionEditor, System.Design", "System.Drawing.Design.UITypeEditor, System.Drawing")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public List<string> TestList { get; set; }

public ListTest()
{
    TestList = new List<string>();
}