Dear friends
i have basic doubt that is
can we add ListItem's to ListBox (or) is ListItem's are only for ListView
any suggetions
Thanks
Ranadheer
Windows Forms General
Dear friends
i have basic doubt that is
can we add ListItem's to ListBox (or) is ListItem's are only for ListView
any suggetions
Thanks
Ranadheer
Dear Matthew
I have added ListItem objects to my ListBox
it working fine.
but i am getting Exceptions when i try to remove that ListItem Objects from my ListBox
the exception is
"List that this enumerator is bound to has been modified. An enumerator can only be used if the list does not change."
any suggetion.
Regards
Ranadheer
It is tricky to remove list items in an iteration statement, since the collection gets modified when an item is removed.
Please see if this code helps
for (; listBox1.SelectedIndices.Count > 0;)
{
listBox1.Items.RemoveAt(listBox1.SelectedIndices[0]);
}
You can add anything to ListBox because it accepts an object and every type in .Net is somehow directly or indirectly inherits from the object class.
It better to Inherit from ListItem and override its ToString() method so atleast you can have meaningful view of its in ListBox:
Here:
public class MyListItem : ListViewItem
{
public override ToString()
{
return this.Text;
}
}
I hope this will help.
Best Regards,
Rizwan aka RizwanSharp
ranadheer mac wrote:
Dear Matthew
I have added ListItem objects to my ListBox
it working fine.
but i am getting Exceptions when i try to remove that ListItem Objects from my ListBox
the exception is
"List that this enumerator is bound to has been modified. An enumerator can only be used if the list does not change."
any suggetion.
Regards
Ranadheer
You are using foreach loop to remove items replace your loop to be for or any other loop but not the foreach loop.
I hope this will solve your problem.
Best Regards,
Rizwan aka RizwanSharp
Matthew Watson wrote:
That would be ok if it was a ListView the OP is using, but I think he's using a ListBox.
It'll work for ListBox too because see this signature ListBox.Items.Add(object obj);
So you can put any object of any type in ListBox.
Best Regards,
Rizwan aka RizwanSharp.