locked
How to define collection of collection in class library as property to use at design time RRS feed

  • Question

  • i'm using c# class library to create a custom control. i have no problem with simple collection of type, this is an example how i use it:

    namespace PersoComboBox
    {
    
        public class ComboColDisplay
        {
            private string name;
            private int percentage;
    
            public string ColName
            {
                get { return this.name; }
                set { this.name = value; }
            }
    
            public int ColPercent
            {
                get { return this.percentage; }
                set { this.percentage = value; }
            }
    
            public ComboColDisplay()
            {
            }
        }
    
        public class PersoComboBox : ComboBox 
        {
            //Variables locales
            ErrorProvider EP = new ErrorProvider();
            //Collection property
            private List<ComboColDisplay> listColDisplay = new List<ComboColDisplay>();
    
    
            [Category("xControl properties")]
            [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
            public List<ComboColDisplay> PListColumnDisplay
            {
                get { return listColDisplay; }
                set { listColDisplay = value; Invalidate(); }
            }
    
    
            public PersoComboBox()
            {
            }
    
            private void refreshDrawMode()
            {
                if (listColDisplay.Count > 0) { this.DrawMode = DrawMode.OwnerDrawVariable; }
                else { this.DrawMode = DrawMode.Normal; }
            }
        }
    }

    Now i want to use collection of collection of type as a property ! how can i do it ?

    Thanks.

    Monday, April 20, 2020 9:00 PM

Answers

  • IMO, the class in the classlib project would have a custom type defined, it would have a method that would load the list with the custom type and return the list of the custom type to the control with the control sitting in the Windows form project. Becuase you have a reference to the classlib project from the Windows form project, then both projects know about the custom type that's in the list.

    comBox1.DataSource = new Library.GetList();
     
    in Library you have a method.
    
    public List<Person> GetList()
    {
        var list = new List<Person>();
        create new Person
        add new Person to list
        return list;
    }

    https://www.c-sharpcorner.com/UploadFile/0f68f2/programmatically-binding-datasource-to-combobox-in-multiple/

    • Marked as answer by MohanedAmine Tuesday, April 21, 2020 9:37 AM
    Tuesday, April 21, 2020 4:29 AM
  • Just becuase you placed the  [serializable] attribute over a class does it mean that it can be serialized. In this situation,  a Control, a class,  can't be serialized becuase you can't get the attribute over the control class itself.

    https://docs.microsoft.com/en-us/dotnet/api/system.serializableattribute?view=netframework-4.8

    <copied>

    Apply the SerializableAttribute attribute to a type to indicate that instances of this type can be serialized. The common language runtime throws SerializationException if any type in the graph of objects being serialized does not have the SerializableAttribute attribute applied.

    <end>

    IMO, what you're trying to do is not viable.

    • Marked as answer by MohanedAmine Wednesday, April 22, 2020 10:42 AM
    Tuesday, April 21, 2020 4:45 PM
  • The only thing I think you can do is create a folder in the Windows form project and place your class there and try to use it. I think that trying to place the class in a classlib project and trying to bring the object across project boundaries is causing .NET to want the object serializable that can't be done. If the class/object is in the Winnows form project, at least it is not traveling between projects sort of speaking. 
    • Marked as answer by MohanedAmine Thursday, April 23, 2020 6:32 PM
    Wednesday, April 22, 2020 4:07 PM

All replies

  • IMO, the class in the classlib project would have a custom type defined, it would have a method that would load the list with the custom type and return the list of the custom type to the control with the control sitting in the Windows form project. Becuase you have a reference to the classlib project from the Windows form project, then both projects know about the custom type that's in the list.

    comBox1.DataSource = new Library.GetList();
     
    in Library you have a method.
    
    public List<Person> GetList()
    {
        var list = new List<Person>();
        create new Person
        add new Person to list
        return list;
    }

    https://www.c-sharpcorner.com/UploadFile/0f68f2/programmatically-binding-datasource-to-combobox-in-multiple/

    • Marked as answer by MohanedAmine Tuesday, April 21, 2020 9:37 AM
    Tuesday, April 21, 2020 4:29 AM
  • Hi,

    I want that this custom control with such a property, as in the following code run properly :

    namespace responsiveContainer
    {
        public class ctrlGroup
        {
            private List<sortedCtrl> listCtrl = new List<sortedCtrl>();
            private int rowNumber;
    
            public List<sortedCtrl> listCtrlin
            {
                get { return this.listCtrl; }
                set { this.listCtrl = value; }
            }
            public int row_number
            {
                get { return this.rowNumber; }
                set { this.rowNumber = value; }
            }
    
            public ctrlGroup()
            {
            }
        }
    
        public class sortedCtrl
        {
            private Control ctrl;
            private int overLevel;
    
            public Control ctrlin
            {
                get { return this.ctrl; }
                set { this.ctrl = value; }
            }
            public int over_level
            {
                get { return this.overLevel; }
                set { this.overLevel = value; }
            }
    
            public sortedCtrl()
            {
            }
        }
    
        public class scrollable_responsive_container:XtraScrollableControl
        {
            private List<ctrlGroup> rowGroupControls = new List<ctrlGroup>();
    
            //property who has a problem
            [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
            [Category("xControl properties")]
            public List<ctrlGroup> sorted_ctrls_inPanel
            {
                get { return rowGroupControls; }
                set { rowGroupControls = value; }
            }
    
            public scrollable_responsive_container()
            {
    
            }
    
            protected override void OnSizeChanged(EventArgs e)
            {
                base.OnSizeChanged(e);
            }
        }
    }

    it seems to be good ! but i have an error at design time : type responsiveContainer.sortedCtrl in assembly 'responsiveContainer, version 1.0.0.0, cultur = neutral, publicKeyToken = null' is not marked as serializable !

    i tested : [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)], i haven't error massage at design time but the list will be empty at run time !

    I also added [Serializable] above "public class sortedCtrl" the error code disappear but come back with any control inside my custom control !

    how can i solve the problem ?




    Tuesday, April 21, 2020 11:28 AM
  • Just becuase you placed the  [serializable] attribute over a class does it mean that it can be serialized. In this situation,  a Control, a class,  can't be serialized becuase you can't get the attribute over the control class itself.

    https://docs.microsoft.com/en-us/dotnet/api/system.serializableattribute?view=netframework-4.8

    <copied>

    Apply the SerializableAttribute attribute to a type to indicate that instances of this type can be serialized. The common language runtime throws SerializationException if any type in the graph of objects being serialized does not have the SerializableAttribute attribute applied.

    <end>

    IMO, what you're trying to do is not viable.

    • Marked as answer by MohanedAmine Wednesday, April 22, 2020 10:42 AM
    Tuesday, April 21, 2020 4:45 PM
  • Thank you very much for your appreciated answers

    So please how can I create a Collection property (inside my class library custom control) which itself contains another collection of controls, properly ?
    44/5000
    thank you very much for your appreciated answers
    44/5000
    thank you very much for your appreciated answers
    Wednesday, April 22, 2020 10:44 AM
  • The only thing I think you can do is create a folder in the Windows form project and place your class there and try to use it. I think that trying to place the class in a classlib project and trying to bring the object across project boundaries is causing .NET to want the object serializable that can't be done. If the class/object is in the Winnows form project, at least it is not traveling between projects sort of speaking. 
    • Marked as answer by MohanedAmine Thursday, April 23, 2020 6:32 PM
    Wednesday, April 22, 2020 4:07 PM
  • Thank you very much 
    Thursday, April 23, 2020 6:34 PM