C# How to use List Class as Property of Custom Form
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:
c# visual-studio propertyeditor
add a comment |
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:
c# visual-studio propertyeditor
2
What value doesParametersList
have? Why are you inheriting from genericList<Parameter>
AND maintaining as new instance of aList<Parameter>
inside the custom implementation? Just get rid of that type and useList<Parameter>
directly:public List<Parameter> Parameters { get; set; }
– Igor
Dec 3 at 14:43
It is recommended by Microsoft not to inherit fromList<T>
but fromSystem.Collections.ObjectModel.Collection<T>
.
– ja72
Dec 3 at 16:46
add a comment |
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:
c# visual-studio propertyeditor
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:
c# visual-studio propertyeditor
c# visual-studio propertyeditor
edited Dec 3 at 14:42
Peter B
12.7k51941
12.7k51941
asked Nov 11 at 17:56
Ray Mart Morandarte
62
62
2
What value doesParametersList
have? Why are you inheriting from genericList<Parameter>
AND maintaining as new instance of aList<Parameter>
inside the custom implementation? Just get rid of that type and useList<Parameter>
directly:public List<Parameter> Parameters { get; set; }
– Igor
Dec 3 at 14:43
It is recommended by Microsoft not to inherit fromList<T>
but fromSystem.Collections.ObjectModel.Collection<T>
.
– ja72
Dec 3 at 16:46
add a comment |
2
What value doesParametersList
have? Why are you inheriting from genericList<Parameter>
AND maintaining as new instance of aList<Parameter>
inside the custom implementation? Just get rid of that type and useList<Parameter>
directly:public List<Parameter> Parameters { get; set; }
– Igor
Dec 3 at 14:43
It is recommended by Microsoft not to inherit fromList<T>
but fromSystem.Collections.ObjectModel.Collection<T>
.
– ja72
Dec 3 at 16:46
2
2
What value does
ParametersList
have? Why are you inheriting from generic List<Parameter>
AND maintaining as new instance of a List<Parameter>
inside the custom implementation? Just get rid of that type and use List<Parameter>
directly: public List<Parameter> Parameters { get; set; }
– Igor
Dec 3 at 14:43
What value does
ParametersList
have? Why are you inheriting from generic List<Parameter>
AND maintaining as new instance of a List<Parameter>
inside the custom implementation? Just get rid of that type and use List<Parameter>
directly: public List<Parameter> Parameters { get; set; }
– Igor
Dec 3 at 14:43
It is recommended by Microsoft not to inherit from
List<T>
but from System.Collections.ObjectModel.Collection<T>
.– ja72
Dec 3 at 16:46
It is recommended by Microsoft not to inherit from
List<T>
but from System.Collections.ObjectModel.Collection<T>
.– ja72
Dec 3 at 16:46
add a comment |
2 Answers
2
active
oldest
votes
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.
add a comment |
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; }
}
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:
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53251552%2fc-sharp-how-to-use-list-class-as-property-of-custom-form%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
add a comment |
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.
add a comment |
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.
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.
answered Dec 3 at 15:26
D Stanley
122k9113174
122k9113174
add a comment |
add a comment |
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; }
}
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:
add a comment |
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; }
}
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:
add a comment |
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; }
}
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:
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; }
}
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:
answered Dec 3 at 17:54
ja72
18k347101
18k347101
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53251552%2fc-sharp-how-to-use-list-class-as-property-of-custom-form%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
What value does
ParametersList
have? Why are you inheriting from genericList<Parameter>
AND maintaining as new instance of aList<Parameter>
inside the custom implementation? Just get rid of that type and useList<Parameter>
directly:public List<Parameter> Parameters { get; set; }
– Igor
Dec 3 at 14:43
It is recommended by Microsoft not to inherit from
List<T>
but fromSystem.Collections.ObjectModel.Collection<T>
.– ja72
Dec 3 at 16:46