How to properly open the List <object> property in Visual Studio C #?

I created a property for the custom window shape that I created.

private List<object> values; public List<object> Values { get { return values; } set { values = value; } } 

It is displayed in the properties window on the designer just fine. I go to the property value field, and the button with "..." shows three dots. I click on the button and a window appears with which you can add items to the list. I add them and click OK. Erro does not appear, but the items were not saved.

My question is how to properly configure this so that I can set the List<object> elements in the properties window at design time?

+4
source share
2 answers

In your Form1.Designer.cs manually create an instance of List , like this

 this.Values = new List<object>(); 

After you added the elements, the Form1.Designer.cs file will be recreated as normal, but the line above will be replaced by

 this.Values = ((System.Collections.Generic.List<object>)(resources.GetObject("$this.Values"))); 

Alternatively, instantiate the list when you declare it.

 private List<object> values = new List<object>(); public List<object> Values { get { return values; } set { values = value; } } 
+1
source

thanks to keyboardP. I changed my code to what you suggested

private List <object> values ​​= new List <object> ();

public List <object> Values ​​{get {return values; } set {values ​​= value; }}

It works exactly as I wanted.

One remark in case someone needs it. If you use your own class, for example List <CustomClass> instead of List <object> . In the definition of "CustomClass," do this

[System.Serializable] public class CustomClass { ...... }

Otherwise, you will receive an error message when you try to add items to the list through the properties window.

Another method would also change the List <CustomClass> to CustomClass []

private values ​​CustomClass [];

public CustomClass [] Values ​​{get {return values; } set {values ​​= value}}

This second method I did not need to add [System.Serializable] at the beginning of the CustomClass definition.

Hope this helped someone.

+1
source

Source: https://habr.com/ru/post/1490053/


All Articles