SelectedIndexChanged not fired by bound listbox when starting deleting first entriesSelectedIndexChanged not fired by bound listbox when starting deleting first entries
Old forum URL: forums.lhotka.net/forums/t/2655.aspx
stefan posted on Wednesday, April 04, 2007
Hi,
I have a Listbox bound to a BusinessListBase through a bindingsource.
There is a button near, that enables the user to delete items from the BusinessListBase.
private void btnDeleteCriteria_Click(object sender, EventArgs e)
{
if (_currentCrit != null) _query.criteria.Remove(_currentCrit);
}
The _currentCrit child reference is set in the listbox' SelectedIndexChanged event handler.
The listbox behaves as expected when I choose to delete the last entry from the list.
The selected entry is deleted from the list, and thanks to the SelectedIndexChanged event firing,
_currentCrit is set to the next (= previous) item in the list, so the user could go on deleting from the list.
As soon as I want to delete an item that's not the last in the list, SelectedIndexChanged is not
fired, although the listbox apparently seems to select the next (= next in line) item, which is not
the case. My reference is still set to the previously deleted object.
What am I doing wrong here?
As I wrote this, I realized that actually the index of the selected item is never going to change
when not deleting the last position....dumb me.
Is there any chance of getting a 'SelectedItemChanged' equivalent for the listbox control?
Stefan
Bayu replied on Wednesday, April 04, 2007
Why don't you replace your _currentCrit reference with an _currentIndex variable?
;-)
Bayu
stefan replied on Wednesday, April 04, 2007
Hi Bayu!
Relying on the index doesn't sound as a goog idea to me, because the index of an object could
change. I feel better having an extra reference to the current object, as I need it to hook and unhook
PropertyChanged event handlers on it.
Anyway, I solved my issue by coding the following check for "-1":
private void btnDeleteCriteria_Click(object sender, EventArgs e)
{
if (_currentCrit != null) _query.criteria.Remove(_currentCrit);
// when not deleting the last item in the list, _currentCrit is not reset automatically
// to the current listselection, because 'SelectedIndexChanged' did not fire,
// and _currentCrit still points to the deleted object!
if (criteriaListBox.Items.IndexOf(_currentCrit) == -1)
{
UserQueryCriteria _selectedCrit = criteriaListBox.SelectedItem as UserQueryCriteria;
if (_selectedCrit != null) UpdateCriteriaEditArea(_selectedCrit);
}
}
Thanks for your support!
Stefan
Copyright (c) Marimer LLC