This looks like C# code, but my guess is that checkedListBox1->CheckedItems->Count is decremented when you remove each item. Start at checkedListBox1->CheckedItems->Count and go to 0.
You're almost right: It's C++/CLI. But your proposed solution is right on the spot and should work.
Migeria, Your code is C++/CLI and we have a specific forum for this around here: http://www.codeguru.com/forum/forumdisplay.php?f=17. Your chance to get answers for questions about that language are better there.
But MrViggy already has correctly diagnosed your problem and made a suggestion that should work. Also, we recently had another thread about the same problem: http://www.codeguru.com/forum/showthread.php?t=507532. This is about the MFC CListCtrl though, but it contains some more alternative solutions that should apply to your case as well.
Ah, and... Welcome to CodeGuru!
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
Thanks for both of you Since I'm not native English speaking person, it took some time to get what I should do, but well... in the end I succeeded Y(^-^)Y
So thats how those drugs for headache are called xD C++/CLI. Thanks I will try to remember those random letters.
And here's reference for future generations:
Code:
if(checkedListBox1->CheckedItems->Count != 0)
{
String ^ s;
for(int x = 0; x < checkedListBox1->CheckedItems->Count; checkedListBox1->CheckedItems->Count)
{
s = String::Concat(s, checkedListBox1->CheckedItems[x],"\n");
checkedListBox1->Items->Remove(checkedListBox1->CheckedItems[x]);
}
MessageBox::Show(s, "Istrinti is pasirinkimo saraso");
}
Well, this code works, so this is primarily FYI, but it can be simplified considerably.
The part marked in red in the following line has no effect and can be omitted as well:
Code:
for(int x = 0; x < checkedListBox1->CheckedItems->Count; checkedListBox1->CheckedItems->Count)
You certainly know that this code is meant to advance the loop variable (in this case x), and therefore x always remains 0 during the entire loop. (BTW, the equivalent in the for loop in post #1 actually did what it was supposed to do - and exactly that was what caused the problem.)
As a consequence of this, your code is equivalent to this:
Code:
String ^ s;
while (checkedListBox1->CheckedItems->Count > 0)
{
s = String::Concat(s, checkedListBox1->CheckedItems[0],"\n");
checkedListBox1->Items->Remove(checkedListBox1->CheckedItems[0]);
}
MessageBox::Show(s, "Istrinti is pasirinkimo saraso");
Well, strictly speaking it is not exactly equivalent. To make it that, you can wrap the whole thing in an uncontrolled {} to limit the lifetime of s.
BTW, forums like this one are also a great English training.
Last edited by Eri523; January 18th, 2011 at 07:12 PM.
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
Bookmarks