ranadheer mac

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



Re: Windows Forms General Can we Add ListItem objects to ListBox ?

Matthew Watson

It is only for ListView.

You can add any object to ListBox, but it must have a suitable ToString() method.




Re: Windows Forms General Can we Add ListItem objects to ListBox ?

ranadheer mac

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





Re: Windows Forms General Can we Add ListItem objects to ListBox ?

JDee

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]);

}





Re: Windows Forms General Can we Add ListItem objects to ListBox ?

RizwanSharp

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






Re: Windows Forms General Can we Add ListItem objects to ListBox ?

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






Re: Windows Forms General Can we Add ListItem objects to ListBox ?

Matthew Watson

That would be ok if it was a ListView the OP is using, but I think he's using a ListBox.




Re: Windows Forms General Can we Add ListItem objects to ListBox ?

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.






Re: Windows Forms General Can we Add ListItem objects to ListBox ?

Matthew Watson

Sorry, that's not quite what I meant.

What I really meant is that there's no point in deriving something from ListViewItem.
You only need to implement ToString() for whatever type you want to add.