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:
You want a list of
Parameter
objects in a custom control. This is done simply by providing aList<Parameter>
property on the control. Here is an example using a user form: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: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>
.and you control class
which creates the following: