Assign a Collection class to the items of a list box

3.3k Views Asked by At

I'm sure there should be an easier way to do this. I have a class based on Collection, and that class is a collection of another class. Currently, whenever I have a new item in my class, I assign that item to the listbox. I can't seem to figure out a way to assign all of the values in the collection class, because it is a collection, to the collection of the listbox. Any ideas? Thanks

Ok, what I've done so far is I have a tostring override in the Class used in the collection. This is what I want the listbox to show.

      public override string ToString()
    {

        return string.Format("{0} {1}: {2}", tTypev.ToString(),
                Datev.ToString("MM/dd/yyyy"), Amountv.ToString("C"));
    }

Which is what I want each item in the listbox to show.

                class Transactions : System.Collections.CollectionBase
                {
                     ...
                }

Is my collections class, containing a collection of the other class, Tansaction. Curently, I use the lstTransactions.Items.Add(), .Remove, .RemovAt, etc to add items to the list box, and the .Add(), .Remove, etc to add items to the Collection Class, Transactions. But I'm trying to decrease reliance on outside controls, and only use them in a few lines of code. I was trying to use something like:

lstTransactions.DataSource = (Transaction)myTrans;

but that didn't seem to work. Mainly because I couldn't figure out what property DataSource took.

I also tried:

lstTransactions.Items = 

but it told me that items was read only.

1

There are 1 best solutions below

4
On

In Windows Form:

  1. You could used DataSource property to bind a collection of object.
  2. DisplayMember property to the name of a property in the data source object.

Sample Code: In the below sample the output list box would display 2 items : Apple and Ball.

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            EmployeeCollection data = new EmployeeCollection();
            data.AddEmployee(new Employee("Apple", 25));
            data.AddEmployee(new Employee("Ball", 50));

            listBox1.DataSource = data;
            listBox1.DisplayMember = "Name";
        }

    }

    public class Employee
    {
        public Employee(string name, int age)
        {
            this.Name = name;
            this.Age = age;
        }

        public int Age
        {
            get; private set;
        }

        public string Name
        {
            get; private set;
        }
    }

    public class EmployeeCollection : System.Collections.CollectionBase
    {
        public void AddEmployee(Employee employee)
        {
            this.List.Add(employee);
        }
    }
}

In WPF:

You could use ItemSource property of ListBox to bind a collection (which may be a List, Enumerable, Collections.._ do the job

Sample snippet:

IList<string> data = new List<string>() {"A", "B"};

ListBox listBox = new ListBox();
listBox.ItemsSource = data;