Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.Net WinForm application not persisting a property of type List<MyClass>

I've created a user control in a Windows Application C# 3.5 and it has a number of properties (string, int, color etc). These can be modified in the properties window and the values are persisted without problem.

However I've created a property like

  public class MyItem
  {
       public string Text { get; set; }
       public string Value { get; set; }
  }

  public class MyControl : UserControl
  {
       public List<MyItem> Items { get; set; }
  }

The properties dialog allows me to add and remove these items, but as soon as I close the dialog the values I entered are lost.

What am I missing? Many thanks!

like image 222
Dead account Avatar asked Nov 05 '22 15:11

Dead account


1 Answers

You need to initialise the the Items so an auto getter/setter won't help you here.

Try

public class MyControl : UserControl
{
    private List<MyItem> _items = new List<MyItem>();

    public List<MyItem> Items
    {
         get { return _items; }
         set { _items = value; }
    }
 }
like image 155
Sophie88 Avatar answered Nov 14 '22 22:11

Sophie88