using collectionbase derivative as datasource

D

David J Rose

If I define my own classes for an object "Test" and a collection of those
objects "TestCollection", what do I need to add to the class to let me set
the collection as a datasource for a listbox and have it automatically
update whenever an item is added/edited or removed from the collection?

Here are the classes:

public class Test
{
private int index;
private string value;

public Test(int index, string value)
{
this.index = index;
this.value = value;
}

public int Index
{
get
{
return index;
}
}

public string Value
{
get
{
return value;
}
}

public override string ToString()
{
return value;
}

}


public class TestCollection : CollectionBase
{
public int Add(Test test)
{
return base.List.Add(test);
}

public Test this[int i]
{
get
{
return (Test)base.List;
}
set
{
base.List = value;
}
}

}

Thanks
 
J

Jianwei Sun

Listbox has a property called DataSource, that's where you should look for.

There is an example in MSDN you can look for .

John
 
D

David J Rose

That is not exactly what I mean. If I set the datasource to my collection,
it displays fine, but any changes made to the underlying Test objects do not
refresh the listbox. I understand that you have to implemnet some interface,
but do not no the details or indeed, how to do it.


Jianwei Sun said:
Listbox has a property called DataSource, that's where you should look
for.

There is an example in MSDN you can look for .

John
If I define my own classes for an object "Test" and a collection of those
objects "TestCollection", what do I need to add to the class to let me
set the collection as a datasource for a listbox and have it
automatically update whenever an item is added/edited or removed from the
collection?

Here are the classes:

public class Test
{
private int index;
private string value;

public Test(int index, string value)
{
this.index = index;
this.value = value;
}

public int Index
{
get
{
return index;
}
}

public string Value
{
get
{
return value;
}
}

public override string ToString()
{
return value;
}

}


public class TestCollection : CollectionBase
{
public int Add(Test test)
{
return base.List.Add(test);
}

public Test this[int i]
{
get
{
return (Test)base.List;
}
set
{
base.List = value;
}
}

}

Thanks
 
J

Jianwei Sun

Jianwei said:
Also, I think you need to implement IBindingList on your collection.
David said:
If I define my own classes for an object "Test" and a collection of
those objects "TestCollection", what do I need to add to the class to
let me set the collection as a datasource for a listbox and have it
automatically update whenever an item is added/edited or removed from
the collection?

Here are the classes:

public class Test
{
private int index;
private string value;

public Test(int index, string value)
{
this.index = index;
this.value = value;
}

public int Index
{
get
{
return index;
}
}

public string Value
{
get
{
return value;
}
}

public override string ToString()
{
return value;
}

}


public class TestCollection : CollectionBase
{
public int Add(Test test)
{
return base.List.Add(test);
}

public Test this[int i]
{
get
{
return (Test)base.List;
}
set
{
base.List = value;
}
}

}

Thanks

Dave,

Here is a detalied example from MSDN. Go to MSDN to look it up yourself.

I would like to copy my own code to you, but the company policy won't
allow me to do that.

HTH.


public class CustomersList : CollectionBase, IBindingList
{

private ListChangedEventArgs resetEvent = new
ListChangedEventArgs(ListChangedType.Reset, -1);
private ListChangedEventHandler onListChanged;

public void LoadCustomers()
{
IList l = (IList)this;
l.Add(ReadCustomer1());
l.Add(ReadCustomer2());
OnListChanged(resetEvent);
}

public Customer this[int index]
{
get
{
return (Customer)(List[index]);
}
set
{
List[index] = value;
}
}

public int Add (Customer value)
{
return List.Add(value);
}

public Customer AddNew()
{
return (Customer)((IBindingList)this).AddNew();
}

public void Remove (Customer value)
{
List.Remove(value);
}


protected virtual void OnListChanged(ListChangedEventArgs ev)
{
if (onListChanged != null)
{
onListChanged(this, ev);
}
}


protected override void OnClear()
{
foreach (Customer c in List)
{
c.Parent = null;
}
}

protected override void OnClearComplete()
{
OnListChanged(resetEvent);
}

protected override void OnInsertComplete(int index, object value)
{
Customer c = (Customer)value;
c.Parent = this;
OnListChanged(new
ListChangedEventArgs(ListChangedType.ItemAdded, index));
}

protected override void OnRemoveComplete(int index, object value)
{
Customer c = (Customer)value;
c.Parent = this;
OnListChanged(new
ListChangedEventArgs(ListChangedType.ItemDeleted, index));
}

protected override void OnSetComplete(int index, object oldValue,
object newValue)
{
if (oldValue != newValue)
{

Customer oldcust = (Customer)oldValue;
Customer newcust = (Customer)newValue;

oldcust.Parent = null;
newcust.Parent = this;


OnListChanged(new
ListChangedEventArgs(ListChangedType.ItemAdded, index));
}
}

// Called by Customer when it changes.
internal void CustomerChanged(Customer cust)
{

int index = List.IndexOf(cust);

OnListChanged(new
ListChangedEventArgs(ListChangedType.ItemChanged, index));
}


// Implements IBindingList.
bool IBindingList.AllowEdit
{
get { return true ; }
}

bool IBindingList.AllowNew
{
get { return true ; }
}

bool IBindingList.AllowRemove
{
get { return true ; }
}

bool IBindingList.SupportsChangeNotification
{
get { return true ; }
}

bool IBindingList.SupportsSearching
{
get { return false ; }
}

bool IBindingList.SupportsSorting
{
get { return false ; }
}


// Events.
public event ListChangedEventHandler ListChanged
{
add
{
onListChanged += value;
}
remove
{
onListChanged -= value;
}
}

// Methods.
object IBindingList.AddNew()
{
Customer c = new Customer(this.Count.ToString());
List.Add(c);
return c;
}


// Unsupported properties.
bool IBindingList.IsSorted
{
get { throw new NotSupportedException(); }
}

ListSortDirection IBindingList.SortDirection
{
get { throw new NotSupportedException(); }
}


PropertyDescriptor IBindingList.SortProperty
{
get { throw new NotSupportedException(); }
}


// Unsupported Methods.
void IBindingList.AddIndex(PropertyDescriptor property)
{
throw new NotSupportedException();
}

void IBindingList.ApplySort(PropertyDescriptor property,
ListSortDirection direction)
{
throw new NotSupportedException();
}

int IBindingList.Find(PropertyDescriptor property, object key)
{
throw new NotSupportedException();
}

void IBindingList.RemoveIndex(PropertyDescriptor property)
{
throw new NotSupportedException();
}

void IBindingList.RemoveSort()
{
throw new NotSupportedException();
}

// Worker functions to populate the list with data.
private static Customer ReadCustomer1()
{
Customer cust = new Customer("536-45-1245");
cust.FirstName = "Jo";
cust.LastName = "Brown";
return cust;
}

private static Customer ReadCustomer2()
{
Customer cust = new Customer("246-12-5645");
cust.FirstName = "Robert";
cust.LastName = "Brown";
return cust;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top