1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

C#: For loop not iterating through a list box

Discussion in 'C, C++, C#' started by kytro360, Sep 12, 2011.

  1. kytro360

    kytro360 Power Member

    Joined:
    Jan 12, 2010
    Messages:
    711
    Likes Received:
    734
    I have a listbox filled with several items. I want my program to go down the list and to each item in the listbox do the same action it did for the one above. The code I have goes down the list but it only performs the action for the first item then just goes down the list not doing the action for the other items.

    This is my code:
    Code:
    for (int i = 0; i < numericUpDown2.Value; i++)                        
     {                            
     listBox4.SetSelected(i, true);                             
    listBox4.SelectedItem.ToString();                           
      listBox5.SetSelected(i, true);    
    createaccount();                  
    }
     
  2. mudbutt

    mudbutt Jr. Executive VIP Jr. VIP

    Joined:
    Jun 23, 2010
    Messages:
    1,894
    Likes Received:
    4,418
    Location:
    ghosted
    You'll find more answers on a dedicated programming forum...
     
  3. kytro360

    kytro360 Power Member

    Joined:
    Jan 12, 2010
    Messages:
    711
    Likes Received:
    734
    Well Im asking because this forum has a section specifically for programming, if I shouldnt ask over 50 threads in this section then whats the point of it? I understand what you meant by asking on a programming forum but lately Ive been getting faster more direct, better responses from this forum.
     
  4. mudbutt

    mudbutt Jr. Executive VIP Jr. VIP

    Joined:
    Jun 23, 2010
    Messages:
    1,894
    Likes Received:
    4,418
    Location:
    ghosted
    It was just a suggestion, I remember you blowing up IRC with programming questions, now starting threads. Just thought you'd get better answers from a dedicated programming board that is active.
     
  5. kytro360

    kytro360 Power Member

    Joined:
    Jan 12, 2010
    Messages:
    711
    Likes Received:
    734
    :/ Ok
     
  6. kytro360

    kytro360 Power Member

    Joined:
    Jan 12, 2010
    Messages:
    711
    Likes Received:
    734
    Bump if anyone knows a solution I would appreciate it
     
  7. scriptomania

    scriptomania Junior Member

    Joined:
    Dec 28, 2010
    Messages:
    127
    Likes Received:
    249
    Occupation:
    A full time pirate at sea
    Location:
    The European capital of politics
    Are you sure it actually goes through a full loop? It seems like it's breaking mid-way somewhere, to me. Whats the purpose of "numericupdown2"? And what does createaccount() do exactly?

    What are you trying to achieve here? A more elaborate explanation would help...
    One things for sure though, you problem does not lie in with the loop itself.

    Cheers
     
  8. kytro360

    kytro360 Power Member

    Joined:
    Jan 12, 2010
    Messages:
    711
    Likes Received:
    734
    My program has a list box and the numericupdown value will add an amount of text to the listbox. One item for each number in the numericupdown. When my user clicks start I want it to perform the webrequest (createaccount) for each item in the list.
     
  9. Subsonic

    Subsonic Regular Member

    Joined:
    Mar 17, 2011
    Messages:
    367
    Likes Received:
    333
    Location:
    DNS root zone database
    If you want to iterate through all the items in the listbox you should do it like this:

    for(int i = 0; i < listBox1.Items.Count; i++)

    Now if you want to get the value of each item in the listbox you should do this:

    string currentValue = listBox1.Items.Value.ToString();
     
    Last edited: Sep 13, 2011
  10. kytro360

    kytro360 Power Member

    Joined:
    Jan 12, 2010
    Messages:
    711
    Likes Received:
    734
    Awesome so it should be like this:

    Code:
                        for (int i = 0; i < listBox4.Items.Count; i++)
                                {
                                    listBox4.SetSelected(i, true);
                                    listBox4.SelectedItem.ToString();
                                    listBox5.SetSelected(i, true);
                                    listBox5.SelectedItem.ToString();
                                    createaccount();
    }
                     
    
    
     
  11. kytro360

    kytro360 Power Member

    Joined:
    Jan 12, 2010
    Messages:
    711
    Likes Received:
    734
    EDIT: I tried your code but it didnt perform the method createaccount and it went through it twice not once. In my other loop with numericupdown instead of listBox1.Items.Count it went through the method :/
     
  12. Subsonic

    Subsonic Regular Member

    Joined:
    Mar 17, 2011
    Messages:
    367
    Likes Received:
    333
    Location:
    DNS root zone database
    No, I posted two corrections, you didn't add the second one. You shouldn't select the items and then call selectedItem property because it's just way too time consuming and plain wrong.

    If you want to get the content of any item in a listbox you do it like this:

    string currentValue = listBox1.Items[INDEX_HERE].Value.ToString();
     
  13. Subsonic

    Subsonic Regular Member

    Joined:
    Mar 17, 2011
    Messages:
    367
    Likes Received:
    333
    Location:
    DNS root zone database
    Well, you're doing something wrong because you will go through all the items on the listbox ONCE by using the following for-statement:

    for(int i = 0; i < listBox1.Items.Count; i++) {

    // SOME CODE HERE

    }
     
  14. kytro360

    kytro360 Power Member

    Joined:
    Jan 12, 2010
    Messages:
    711
    Likes Received:
    734
    Quick question. Is it necessary for me to have:
    Code:
                  for (int i = 0; i < listBox4.Items.Count; i++)                             {                                 listBox4.SetSelected(i, true);                                 listBox4.SelectedItem.ToString();                                 listBox5.SetSelected(i, true);                                 listBox5.SelectedItem.ToString();                                 createaccount(); }
    or is:

    Code:
    [B]for(int i = 0; i < listBox1.Items.Count; i++) {
    
    // SOME CODE HERE
    
    }[/B]
    correct and will do the same thing?
     
  15. kytro360

    kytro360 Power Member

    Joined:
    Jan 12, 2010
    Messages:
    711
    Likes Received:
    734
    I think its not the loop. There was an issue with the webrequest.
     
  16. PinkyX

    PinkyX Power Member

    Joined:
    Feb 25, 2011
    Messages:
    577
    Likes Received:
    250
    Location:
    EU
    hahaha what a fuckin nooooob
     
    • Thanks Thanks x 1
  17. kytro360

    kytro360 Power Member

    Joined:
    Jan 12, 2010
    Messages:
    711
    Likes Received:
    734
    I tested it out more thoroughly and both loops only perform the action to the first item then go down the list not performing the action for the other items.
     
  18. HatIsBlack

    HatIsBlack Regular Member

    Joined:
    Sep 17, 2010
    Messages:
    267
    Likes Received:
    187
    Location:
    Where i belong

    If listbox4 and listbox5 contains the same number of items i would go like this

    Code:
    for (int i = 0; i < listBox4.Items.Count; i++)
    {
    listBox4.SelectedIndex = i;
    listBox5.SelectedIndex = i;
    createaccount();
    }
    
    
    
    [/
     
  19. kytro360

    kytro360 Power Member

    Joined:
    Jan 12, 2010
    Messages:
    711
    Likes Received:
    734
    Since I call the selected items in a string in my method how would I put that in the for loop @HatIsBlack
     
  20. HatIsBlack

    HatIsBlack Regular Member

    Joined:
    Sep 17, 2010
    Messages:
    267
    Likes Received:
    187
    Location:
    Where i belong
    I'm not qiute sure what you mean by call the selected items.

    Do you want to retrive the value of the selected item from the listbox?


    if so then use:
    strMyValue = listbox4.Items(i).ToString();

    if outside loop use:
    strMyValue = listbox4.Items(listbox4.SelectedIndex).ToString();