CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jan 2011
    Location
    Vilnius/Utena, Lithuania
    Posts
    14

    [RESOLVED] checkedlistbox remove

    I'm trying to remove selected items. So far I'm at
    Code:
    			 if(checkedListBox1->CheckedItems->Count != 0)
    			 {
    				 String ^ checked;
    				 for(int i = 0; i < checkedListBox1->CheckedItems->Count; i++)
    				 {
    					 checked = String::Concat(checked, checkedListBox1->CheckedItems[i],"\n");
    					 checkedListBox1->Items->Remove(checkedListBox1->CheckedItems[i]);
    				 }
    				 MessageBox::Show(checked, "Deleted from list");
    			 }
    But it deletes only HALF of selected items.
    Seriously, wth is wrong here? (O.o)

  2. #2
    Join Date
    Feb 2002
    Posts
    4,640

    Re: checkedlistbox remove

    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.

    Viggy

  3. #3
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: checkedlistbox remove

    Quote Originally Posted by MrViggy View Post
    This looks like C# code [...].
    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.

  4. #4
    Join Date
    Jan 2011
    Location
    Vilnius/Utena, Lithuania
    Posts
    14

    Resolved Re: checkedlistbox remove

    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");
    }

  5. #5
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: [RESOLVED] checkedlistbox remove

    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 08: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.

  6. #6
    Join Date
    Jan 2011
    Location
    Vilnius/Utena, Lithuania
    Posts
    14

    Re: [RESOLVED] checkedlistbox remove

    Quote Originally Posted by Eri523 View Post
    you can wrap the whole thing in an uncontrolled {} to limit the lifetime of s.
    could you explain or show example of what is "uncontrolled {}".
    I think I got it wrong, but I can't find any reference on G**gle.

  7. #7
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: [RESOLVED] checkedlistbox remove

    Quote Originally Posted by Migeria View Post
    could you explain or show example of what is "uncontrolled {}".
    The "uncontrolled block" is the outermost pair of {} in the following code snippet, a modified version of the snippet from my last post:

    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");
    }
    I call it "uncotrolled" because it is not dependent on a loop construct or, like in your code in post #1, an if statement. Its use is, as I already memtioned above, while it doesn't influence the control flow as such, it limits the lifetime of the variable s.

    I think I got it wrong, but I can't find any reference on G**gle.
    I'm not sure wether this term is "canonical" enough to be easily used as a search term for a search engine.

    BTW "Google" is not one of the "evil words" here on GodeGuru (while the name of at least one other popular site is). To the contrary: It's a quite common recommendation here to try this before asking a question here.
    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.

  8. #8
    Join Date
    Jan 2011
    Location
    Vilnius/Utena, Lithuania
    Posts
    14

    Re: [RESOLVED] checkedlistbox remove

    Now that was easy to understand ^-^
    I thought that {} could be used only with conditions, loops, function, classes, structures and so on~~~~
    Quote Originally Posted by Eri523 View Post
    It's a quite common recommendation here to try this before asking a question here.
    G**gle word is evil itself =D 'cause sometimes it doesn't find what I need

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured