locked
ListView - Items->Append in for loop

    Question

  • hello,

    i am trying to append items to a ListView using a for-loop like this:

    	if ( passvault.vector_size() > 0) {
    		int p(0);
    		for( p; p < passvault.vector_size(); p++ );
    		{
    			ListView_Connections->Items->Append( passvault.get_resource(p-1) ); 
    		}
    	}

    but I only get one Item appended, even if there are more elements in the vector. Does I have to do something special when using Items->Append within a for loop.

    Thanks in advance!

    br

    David

    Tuesday, April 16, 2013 7:37 PM

Answers

  • The problem is you are ending your for loop with ;

    This terminates the statement and p is out of scope for subsequent references. In your original code, what you thought was the loop block only gets executed once - it increments p to 1 and then calls Append only once.

    Should be:

    		for( int p=0; p < passvault.vector_size(); p++ )
    		{
    			ListView_Connections->Items->Append( passvault.get_resource(p) ); 
    		}
    	


    Tuesday, April 16, 2013 8:37 PM

All replies

  • Nothing special from the Append side. If you trace through the code do you see Append called for all of the items?

    --Rob

    Tuesday, April 16, 2013 8:08 PM
    Owner
  • i also tried this code so that i can get sure that Append is executed multiple times...

    		for( int x = 0; x <= 10; x++ );
    		{
    			ListView_Connections->Items->Append( "test" ); 
    			
    		}

    But even here, i only get one Item.

    If you put in 

    ListView_Connections->Items->Append( "test" );
    ListView_Connections->Items->Append( "test" );
    ListView_Connections->Items->Append( "test" );

    i get the three items...

    this is kind of strange...

    Tuesday, April 16, 2013 8:19 PM
  • Like Rob said, make sure that Append is really being called the number of time you expect it to be. Also, why are you initializing p outside of the for loop and why are you decrementing it's value by 1 in get_resource()? That would make the initial value -1. That just looks odd to me.
    Tuesday, April 16, 2013 8:19 PM
  • i am running this code in 

    MainPage::MainPage()
    {
    	InitializeComponent();
    ...
    }

    i hope someone can give me a hint. thanks!

    br

    Tuesday, April 16, 2013 8:20 PM
  • -1 because passvault.get_resource is returing a vector element (so it needs to start at 0).

    if i use this code...

    for( int p(0); p < passvault.vector_size(); p++ );

    i get an error that p is not defined for the following line...

    ListView_Connections->Items->Append( passvault.get_resource(p-1) ); 

    thanks

    Tuesday, April 16, 2013 8:26 PM
  • The problem is you are ending your for loop with ;

    This terminates the statement and p is out of scope for subsequent references. In your original code, what you thought was the loop block only gets executed once - it increments p to 1 and then calls Append only once.

    Should be:

    		for( int p=0; p < passvault.vector_size(); p++ )
    		{
    			ListView_Connections->Items->Append( passvault.get_resource(p) ); 
    		}
    	


    Tuesday, April 16, 2013 8:37 PM
  • thanks!

    little mistakes make big problems ;)

    maybe i should go to sleep... it´s nearly 11pm at my site.

    thanks a lot!

    Tuesday, April 16, 2013 8:41 PM