C# How to use List Class as Property of Custom Form

923 views Asked by At

I am trying to create a Property made up of List<> for Custom Form. Please take a look at my code below:

//Property of Custom Form
public ParametersList Parameters { get; set; }

public class ParametersList : List<Parameter>
{
    private List<Parameter> parameters = new List<Parameter>();
    public void AddParameter(Parameter param)
    {
        parameters.Add(param);
    }
}

public class Parameter
{
    public String Caption { get; set; }
    public String Name { get; set; }
}

The Property Parameters now appear on a custom form, but the problem is when I click the Ellipsis of the Parameters property and add some list, the list is not saving when I press the Ok button. So every time I press the Ellipsis, the list is clear.

Here is an example of what I am trying to achieve:
image

2

There are 2 answers

2
John Alexiou On BEST ANSWER

You want a list of Parameter objects in a custom control. This is done simply by providing a List<Parameter> property on the control. Here is an example using a user form:

public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();

        ParameterList = new List<Parameter>();
    }

    [Category("Custom")]
    [Description("A list of custom parameters.")]
    public List<Parameter> ParameterList { get; }
}

scr1

Your main issue is that items you add in the list while designing the form, do not persist when the application is run. This is to be expected because the designer does not save the full design state of the controls in a form. It mainly saves the location, names and styles but not the contents.

You will need to fill the list when the form loads, either from a file, a database or programmatically. This should be done in the OnLoad() method:

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        ParameterList.Add(new Parameter() { Name="First", Caption="The first parameter" });
    }

for something like this, I prefer serialization into an XML file which loads automatically when the form is loaded and saves automaticall when the form closes. But that is a topic of discussion on a different question.

You can improve the visuals by creating a custom list class to use instead of List<Parameter>.

[TypeConverter(typeof(ExpandableObjectConverter))]
public class CustomParameterList : System.Collections.ObjectModel.Collection<Parameter>
{
    public override string ToString() => $"List With {Count} Items.";
    public void Add(string name, string caption) => Add(new Parameter() { Name = name, Caption = caption });
}

and you control class

public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();

        ParameterList = new CustomParameterList();
    }

    [Category("Custom")]
    [Description("A list of custom parameters.")]
    public CustomParameterList ParameterList { get; }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        ParameterList.Add("First", "The first parameter");
    }

}

which creates the following:

scr2

scr3

0
D Stanley On

Igor's comment identifies the problem, just use a List<Parameter> and not a custom class. Here's why I think that's the problem:

Your form is adding items to the ParametersList, NOT to the private List<Parameter> inside of the ParametersList.

So your class is a list of parameters (via inheritance), AND has a list of parameters (via the encapsulation). Seems like all you need is to store a collection of parameters, so I don't see the need for a custom class at all.