| I second that arrghh. I like the idea of ICancelAddNew on the list, but
| then this happens.
Ahaaah, but I got it to work whilst you were thinking about it !! :-))
| What if you store the index of the lastly added object (lastNewIndex). Then
| when one of the ICancelAddNew members is called with an index you compare it
| with lastNewIndex. If it isn't the lastly added object then ignore the
| calls. If it is the index of the lastly added object then do what you have
| to do (cancel/accept) and then set your lastNewIndex to -1. Just an idea
Well to start with, I set up a pivate field that would hold a reference to
the latest object and compared the last item in the list against that in the
CancelNew, but your solution ends up being much more efficient as it only
compares integers and not pointers :-))
So I ended up with the following :
{
public class ... : IBindingList, ICancelAddNew
{
...
private int lastNewIndex = -1;
object IBindingList.AddNew()
{
T result = new T();
lastNewIndex = list.Count;
list.Add(result);
OnListChanged(ListChangedType.ItemAdded, lastNewIndex);
return result;
}
void ICancelAddNew.CancelNew(int itemIndex)
{
if (itemIndex == lastNewIndex)
{
list.RemoveAt(itemIndex);
OnListChanged(ListChangedType.ItemDeleted, itemIndex);
lastNewIndex = -1;
}
}
void ICancelAddNew.EndNew(int itemIndex)
{
OnListChanged(ListChangedType.ItemChanged, itemIndex);
}
...
}
}
Works a treat, many thanks for the boost.
Joanna

Signature
Joanna Carter [TeamB]
Consultant Software Engineer
Joanna Carter [TeamB] - 16 Nov 2005 15:40 GMT
| void ICancelAddNew.EndNew(int itemIndex)
| {
| OnListChanged(ListChangedType.ItemChanged, itemIndex);
| }
Now modified to avoid multiple calls ...
void ICancelAddNew.EndNew(int itemIndex)
{
if (itemIndex == lastNewIndex)
{
lastNewIndex = -1;
OnListChanged(ListChangedType.ItemChanged, itemIndex);
}
}
Thanks again
Joanna

Signature
Joanna Carter [TeamB]
Consultant Software Engineer
Bart Mermuys - 16 Nov 2005 16:18 GMT
Hi,
> | void ICancelAddNew.EndNew(int itemIndex)
> | {
[quoted text clipped - 9 lines]
> lastNewIndex = -1;
> OnListChanged(ListChangedType.ItemChanged, itemIndex);
Doesn't this need to be OnListChanged(ListChangedType.ItemAdded, itemIndex).
AFAIK you need to call ItemAdded a second time if the new item is accepted,
not ItemChanged.
> }
> }
>
> Thanks again
You're welcome.
Greetings
> Joanna
Joanna Carter [TeamB] - 16 Nov 2005 16:37 GMT
| Doesn't this need to be OnListChanged(ListChangedType.ItemAdded, itemIndex).
| AFAIK you need to call ItemAdded a second time if the new item is accepted,
| not ItemChanged.
No, it would appear that EndNew(...) is called after you have finished
editing the item that you have already added and notified in AddItem.
Therefore all you need to do is to ensure that the current row is
up-to-date.
Anyway, it really works very nicely now :-))
Joanna

Signature
Joanna Carter [TeamB]
Consultant Software Engineer