Search code examples
c#winformslistbox

Populate listbox with objects, but let it show a concatenation of several properties of said object


I have a form containing several listboxes, populated by objects. One of these listboxes is supposed to contain objects, but show a sentence which is a concatenation of several properties of this object with words in between.

I have already used the override ToString method for displaying this same class in another listbox in a different way. I tried using databinding, but it seems unsuitable for displaying a sentence containing properties of an object. I do have a method in said class that is meant to create this sentence out of the needed properties, but I cannot use this method to populate the listbox, because then the listbox does not contain the objects.

How can I populate this listbox with objects, but let it show said info?

This is the class from which the objects for the listbox come:

public House(string name, string adress, int nrOfStudents)
        {
            this.name = name;
            this.adress = adress;
            this.nrOfStudents = nrOfStudents;
            taskpackages = new List<string>();
            students = new List<Student>();
        }

This is the method which gives an idea of which info should be displayed in the listbox:

public string GetHouseNameStudentsTasks()
        {
            List<string> studentNames = GetStudentNames();
            return this.name + "\tTaskpackages: " + string.Join(", ", taskpackages) + "\tStudents: " + string.Join(", ", studentNames);
        }

The method in the form that is supposed to fulfill the action:

private void btnSaveHouse_Click(object sender, EventArgs e)
        {
            House selectedHouse = lbHouses.SelectedItem as House;
            // several irrelevant functions
            lbHousesAllInfo.Items.Add(selectedHouse);
            // How to let lbHousesAllInfo contain House objects,
            // but show the sentence described in the method above?
        }

Solution

  • Add properties to your class House which will return display texts suitable for each of your list boxes. Example,

    class House
    {
        string name;
        string address;
    
        public House(string name, string address)
        {
            this.name = name;
            this.address = address;
        }
    
        public string DisplayTextForListBox1
        {
            get
            {
                return $"{name}";
            }
        }
    
        public string DisplayTextForListBox2
        {
            get
            {
                return $"{name} {address}";
            }
        }
    }
    

    You will have to use BindingSource component (You can add this component to your form in designer), one for each of your list box control, then set their DataSource properties.

    // 
    // bindingSource1
    // 
    this.bindingSource1.DataSource = typeof(House);
    // 
    // bindingSource2
    // 
    this.bindingSource2.DataSource = typeof(House);
    

    For listBox1 do following settings (You can do it in visual studio designer)

    this.listBox1.DataSource = this.bindingSource1;
    this.listBox1.DisplayMember = "DisplayTextForListBox1";
    

    For listBox2 do following settings

    this.listBox2.DataSource = this.bindingSource2;
    this.listBox2.DisplayMember = "DisplayTextForListBox2";
    

    Add items to binding source components

    private void Button1_Click(object sender, EventArgs e)
    {
        bindingSource1.Add(new House("Name1", "Address 1"));
        bindingSource2.Add(new House("Name2", "Address 2"));
    }