Deleting Items and Selecting next one on ListBox on C#
-
Hi Does anyone know how to delete an item in a ListBox and then, select the next one or previous one in the listbox? Currently, I was using the next code:
int index = listBox1.SelectedIndex;
listBox1.Items.RemoveAt(index);
Reestablecer();
int cuenta = listBox1.Items.Count;
if (index < cuenta)
{
index++;
listBox1.SelectedIndex = index;
}
else
{
index--;
listBox1.SelectedIndex = index;
}but i have a event on SelectedIndexChanged on the ListBox, so when I delete the item in the listbox, automatically the code on the SelectIndexChanged is executed and i get errors because the value of the property listbox.selecteditem is null. Any ideas of how to solve it? thanks in advance. :((
~ Bizarre what men fin attractive ~
-
Hi Does anyone know how to delete an item in a ListBox and then, select the next one or previous one in the listbox? Currently, I was using the next code:
int index = listBox1.SelectedIndex;
listBox1.Items.RemoveAt(index);
Reestablecer();
int cuenta = listBox1.Items.Count;
if (index < cuenta)
{
index++;
listBox1.SelectedIndex = index;
}
else
{
index--;
listBox1.SelectedIndex = index;
}but i have a event on SelectedIndexChanged on the ListBox, so when I delete the item in the listbox, automatically the code on the SelectIndexChanged is executed and i get errors because the value of the property listbox.selecteditem is null. Any ideas of how to solve it? thanks in advance. :((
~ Bizarre what men fin attractive ~
When you have written this code ? Did you try to catch last removed index value in SelectIndexChanged and manipulated your existing code as you mentioned
int cuenta = listBox1.Items.Count;
if (index < cuenta)
{
index++;
listBox1.SelectedIndex = index;
}
else
{
index--;
listBox1.SelectedIndex = index;
}Parwej Ahamad g.parwez@gmail.com
-
Hi Does anyone know how to delete an item in a ListBox and then, select the next one or previous one in the listbox? Currently, I was using the next code:
int index = listBox1.SelectedIndex;
listBox1.Items.RemoveAt(index);
Reestablecer();
int cuenta = listBox1.Items.Count;
if (index < cuenta)
{
index++;
listBox1.SelectedIndex = index;
}
else
{
index--;
listBox1.SelectedIndex = index;
}but i have a event on SelectedIndexChanged on the ListBox, so when I delete the item in the listbox, automatically the code on the SelectIndexChanged is executed and i get errors because the value of the property listbox.selecteditem is null. Any ideas of how to solve it? thanks in advance. :((
~ Bizarre what men fin attractive ~
Your problem is the remove index, when you remove an item you automatically move everything up so don't need to change selected index
-
Hi Does anyone know how to delete an item in a ListBox and then, select the next one or previous one in the listbox? Currently, I was using the next code:
int index = listBox1.SelectedIndex;
listBox1.Items.RemoveAt(index);
Reestablecer();
int cuenta = listBox1.Items.Count;
if (index < cuenta)
{
index++;
listBox1.SelectedIndex = index;
}
else
{
index--;
listBox1.SelectedIndex = index;
}but i have a event on SelectedIndexChanged on the ListBox, so when I delete the item in the listbox, automatically the code on the SelectIndexChanged is executed and i get errors because the value of the property listbox.selecteditem is null. Any ideas of how to solve it? thanks in advance. :((
~ Bizarre what men fin attractive ~
int index = listBox1.SelectedIndex;
if (index >= 0)
{
listBox1.Items.RemoveAt(index);
Reestablecer();
// you only need to change the selected index if it is > the new last item
int lastItem = listBox1.Items.Count - 1;
if (index > lastItem)
{
listBox1.SelectedIndex = lastItem;
}
}"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 -
Your problem is the remove index, when you remove an item you automatically move everything up so don't need to change selected index
I tried to only remove the item, but the program still executes the selectindexchanged statement. I get the error NullRefefenceException.
~ Bizarre what men fin attractive ~
-
int index = listBox1.SelectedIndex;
if (index >= 0)
{
listBox1.Items.RemoveAt(index);
Reestablecer();
// you only need to change the selected index if it is > the new last item
int lastItem = listBox1.Items.Count - 1;
if (index > lastItem)
{
listBox1.SelectedIndex = lastItem;
}
}"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001your code worked fine! Now i just need to check it few more times, because after i delete few items it gets again the null reference exception thank you :) ;)
~ Bizarre what men fin attractive ~
-
your code worked fine! Now i just need to check it few more times, because after i delete few items it gets again the null reference exception thank you :) ;)
~ Bizarre what men fin attractive ~
Try this:
int index = listBox1.SelectedIndex;
if (index >= 0)
{
listBox1.Items.RemoveAt(index);
Reestablecer();
// you only need to change the selected index if it is > the new last item
int lastItem = listBox1.Items.Count - 1;// if there are items in the listbox if (lastItem >= 0) { // if the current index is > than the last item if (index > lastItem) { // change the selected index listBox1.SelectedIndex = lastItem; } } else { // no items in the listbox, so we make the selected index -1 listBox1.SelectedIndex = -1; }
}
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 -
your code worked fine! Now i just need to check it few more times, because after i delete few items it gets again the null reference exception thank you :) ;)
~ Bizarre what men fin attractive ~
Or this:
int index = listBox1.SelectedIndex;
if (index >= 0)
{
listBox1.Items.RemoveAt(index);
Reestablecer();
// you only need to change the selected index if it is > the new last item
int lastItem = listBox1.Items.Count - 1;listBox1.SelectedIndex = Math.Min(lastItem, index);
}
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001