Now this is an interesting question

J

Jax

I have this code, behold:

string message = "Are you sure? You will lose this
customer forever. (Well you'll have re-create from
scratch)";
string caption = "Delete";
MessageBoxButtons buttons = MessageBoxButtons.YesNo;
DialogResult result;
result = MessageBox.Show(this, message, caption, buttons);
if(result == DialogResult.Yes)
{
foreach(ListViewItem lvi in this.lstCustomers.Items)
{
if(lvi.ImageIndex == 1)
{
Customer c = (Customer)lvi.Tag;
alCustomers.Remove(c);
//this.lstCustomers.Items.Remove(lvi);
break;
}
}
}
if(alCustomers.Count<1)
{
this.tabCustomers.Visible = false;
}

Okay what this code does is delete the selected customer
(the one with image an image index of 1) removes the
ListViewItem from the listview then checks to see if there
are no customers left in the array list, if not it hides a
tab control.

What is entertaining is that if you go through this code
with the debugger and remove the slashed out line (where i
remove the listviewitem) alCustomers will never have a
count of zero.
It will for a little while until it hits the slashed out
line, at that point it regains the customer it previously
removed.
Anyone got any ideas why? (I'm assuming it's something to
do with the tag??)

Any help, as always, greatly appreciated, many smiles and
thanks to repliers.

jax
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi Jax,

Try this:

if(result == DialogResult.Yes)
{
ListViewItem tobedeleted = null;
foreach(ListViewItem lvi in this.lstCustomers.Items)
{
if(lvi.ImageIndex == 1)
{
Customer c = (Customer)lvi.Tag;
alCustomers.Remove(c);
tobedeleted = lvi;
break;
}
}
if ( tobedeleted != null )
this.lstCustomers.Items.Remove( tobedeleted);
}

Cheers,
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

I think that it may be either that he is modifying the collection that he
is iterating on:

foreach(ListViewItem lvi in this.lstCustomers.Items)
{
if(lvi.ImageIndex == 1)
{
this.lstCustomers.Items.Remove(lvi);
}
}


Or it may be also due that he is removing the selected element in the
ListView , and he may be handling the ListView.SelectedIndexChanged event.

Jax:
Can you place a breakpoint in the ListView.SelectedIndexChanged handler ?
( if used ). maybe there lay the answer.


Cheers,
 
J

Jon Skeet [C# MVP]

<"Ignacio Machin \( .NET/ C# MVP \)" <ignacio.machin AT
dot.state.fl.us> said:
I think that it may be either that he is modifying the collection that he
is iterating on:

Indeed - hadn't spotted that. I'd have expected to see an exception in
that case...
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi Jon,
Jon Skeet said:
<"Ignacio Machin \( .NET/ C# MVP \)" <ignacio.machin AT


Indeed - hadn't spotted that. I'd have expected to see an exception in
that case...

Yes, I would expect that too, but Jax says nothing about an exception, so I
assume it does not happen.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

 
J

Jax

Jon and Ingacio,

Thank you both so much for your replies, I find your input
very helpful.
Unfortunately this is a work problem and i'm now at home
without the code to hand.
I will post up a sample app on monday when I get back to
it.

As far as exceptions go there are none at all, it runs
without a problem.
My main concern is that in modifying the collection while
iterating through it I have messed up something somewhere
and this is a concern because i have used this code
(usually with arraylists or listboxes) quite a few times.
I assumed that the break keyword saved me from any
problems, is that generally the case?

I will try the new code that ingacio recommended, it
sounds a little bit like the fix I applied which was this:

if(result == DialogResult.Yes)
{
foreach(ListViewItem lvi in this.lstCustomers.Items)
{
if(lvi.ImageIndex == 1)
{
Customer c = (Customer)lvi.Tag;
alCustomers.Remove(c);
break;
}
}
if ( alCustomers.Count == 0 )
this.lstCustomers.Items.Clear();
}

I'll get back to this on monday and see if it effects the
Arraylist when there is more then one customer as i'm not
certain that i've checked that out properly or not(i think
I have but i'll double check, which is why my fix only
applies to 0).

Thanks again, you're both a credit to this newsgroup :)

jax
 
G

Guest

The problem was due to additional code in selected item
changed event.
I sorted it but thanks for your help :)

jax
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top