-3

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.

Chloe
  • 483
  • 4
  • 14

1 Answers1

0

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;
jacob aloysious
  • 2,547
  • 15
  • 16
  • 'System.Windows.Forms.ListBox' does not contain a definition for 'ItemsSource' and no extension method 'ItemsSource' accepting a first argument of type 'System.Windows.Forms.ListBox' could be found (are you messing a using directive or an assembly reference?) ...was the error that it gave me. Any ideas? – Chloe Mar 19 '13 at 17:20
  • @FelesMortis: ItemsSource property was for WPF. I have updated the answer with solution for Windows.Forms. Please try.. – jacob aloysious Mar 20 '13 at 03:31
  • Ok, so I got it working, but I want to add and remove items from the list during runtime. Is there any way to make it dynamically update? – Chloe Mar 21 '13 at 05:25
  • @FelesMortis: You could recreate _data_ object with your new items and reassign the dataSource i.e _listBox1.DataSource = data_ – jacob aloysious Mar 21 '13 at 07:13