For loop not iterating through a list box
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 28

Thread: For loop not iterating through a list box

  1. #1
    Join Date
    Sep 2011
    Posts
    69

    Question For loop not iterating through a list box

    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:


    for (int i = 0; i < numericUpDown2.Value; i++)
    {
    listBox4.SetSelected(i, true);
    listBox4.SelectedItem.ToString();
    listBox5.SetSelected(i, true);
    createaccount();
    }

  2. #2
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,813

    Re: For loop not iterating through a list box

    hmm what is the purpose of the numeric up down if you want to do all the items in the list?

    I assume createaccount is where the action occurs but you are not passing a parameter to the procedure and you did not show the code so we are left to guess.

    First guess would be that the createaccount() is processing the same entry every time.


    btw use [code][/code] tags when posting code
    Always use [code][/code] tags when posting code.

  3. #3
    Join Date
    Sep 2011
    Posts
    69

    Re: For loop not iterating through a list box

    The purpose of the numericupdown is because I use the numericupdown to generate an amount of text before hand. That text goes into the listbox and I want a certain action done to each item in the listbox.

    I am passing a parameter in createaccount() I just didnt include that portion. Im doing a webrequest and I just called the method in the forloop.

  4. #4
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,813

    Re: For loop not iterating through a list box

    That parameter would be the likely source of the problem. Looking at your code it should iterate through the list the number of times of the value of the updown control and call the createaccount() procedure with whatever mystery parameter once for each time through the loop but again since we do not know what is being passed to or being done in the procedure it is hard to say for sure what the problem would be but that would be the place to look.
    Always use [code][/code] tags when posting code.

  5. #5
    Join Date
    Sep 2011
    Posts
    69

    Re: For loop not iterating through a list box

    All the method is a httpwebrequest on a site. I dont know why this should be the focal point of the problem if the loop is in charge of iterating not the method. Its just being called in the loop.

    Would it be necessary to make a separate loop calling the method?

  6. #6
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,813

    Re: For loop not iterating through a list box

    The point is that there is no code in the loop that does anything other than select an item in the list besides the call to create account. So if something is supposed to happen to the items in the list in the createaccount area and it is not happening then where would you think you should look? If it is not supposed to happen in the createaccount routine then where would it happen as there is no other code there or perhaps you left that part out as well?
    Always use [code][/code] tags when posting code.

  7. #7
    Join Date
    Sep 2011
    Posts
    69

    Re: For loop not iterating through a list box

    Im not sure. I didnt create the code, I asked around on another forum and they gave me the code and told me that inside of it to call the method. Im pretty sure I didnt leave a part. Once more all the createaccount method is a httpwebrequest with a site. Any suggestions or maybe ideas to create a better for loop for what I am trying to achieve

  8. #8
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,813

    Re: For loop not iterating through a list box

    I don't know what you are trying to achive. You say perform an action but you do not say what. You say there is a parameter but again you do not say what. You say there is a web request but not a clue what it does so no there is not much of a way to help you do what you are trying to do.
    Always use [code][/code] tags when posting code.

  9. #9
    Join Date
    Sep 2011
    Posts
    69

    Re: For loop not iterating through a list box

    I don't know what you are trying to achive.: My program registers on sites

    You say perform an action but you do not say what.: Go to the site and register

    You say there is a parameter but again you do not say what.: There is no parameter just the webrequest. The method itself looks like this:

    Code:
    public void createaccount ()
    
    {
    
    \\code
    
    
    }
    You say there is a web request but not a clue what it does so no there is not much of a way to help you do what you are trying to do.: Answered this above.

    My program has a list box and the numericupdown value will add an amount of text to the listbox. One item for each numer in the numericupdown. When my user clicks start I want it to perform the webrequest for each item in the list.

  10. #10
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,813

    Re: For loop not iterating through a list box

    If you want to do somethign for each item in the list then you have to do something for each item in the list. Apparently all you are doing is selecting the item and doing nothing with it. You are not passing it to your do something routine as a parameter, you apparently are not referencing the selected item in the list from that do something routine and you are being very secertive about what you are doing which makes me think it may be questionable in nature anyway so I think you are on your own from here.
    Always use [code][/code] tags when posting code.

  11. #11
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    Re: For loop not iterating through a list box

    To explain what you're currently doing:

    Code:
    for (int i = 0; i < numericUpDown2.Value; i++)  //loop from 0 to the current value of the NumericUpDown control
    { 
       listBox4.SetSelected(i, true); //select the item at index 'i' in listBox4
       listBox4.SelectedItem.ToString(); //this line of code does nothing, it is a meaningless statement
       listBox5.SetSelected(i, true); //select the item at index 'i' in listbox5
       createaccount(); //call a method
    }
    This is what you should be doing:

    Code:
    for (int i = 0; i < listBox4.Items.Count; i++)  //loop from 0 to the number of items in listBox4
    {
       createaccount(listBox4.Items[i].ToString()); //call a method using the item at index 'i'
    }
    If needed, you could loop through listBox5 in the same way instead. Does this answer some of your questions?
    It's not a bug, it's a feature!

  12. #12
    Join Date
    Sep 2011
    Posts
    69

    Re: For loop not iterating through a list box

    What if I need to loop through both listBox4 and 5?

  13. #13
    Join Date
    Sep 2011
    Posts
    69

    Re: For loop not iterating through a list box

    @Foamy I tried your code but I got a bunch of errors. Maybe its because my method doesnt take any paramters and the code you provided is for if it did I think.

  14. #14
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,813

    Re: For loop not iterating through a list box

    Yes you have to modify your createaccount routine to accept a parameter and then modify your code in that routine to use the data in the parameter.

    As I have indicated all along the loop is not the issue it is the function which you have not given any detail of.
    Always use [code][/code] tags when posting code.

  15. #15
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    Re: For loop not iterating through a list box

    Quote Originally Posted by kytro360 View Post
    What if I need to loop through both listBox4 and 5?
    Then your design is flawed. You could, however, make sure that both lists have the same amount of items and then loop from 0 to that amount:

    Code:
    int itemCount = listBox4.Items.Count;
    if(itemCount != listBox5.Items.Count)
    {
        //Abort
        return;
    }
    
    for (int i = 0; i < itemCount; i++)  //loop from 0 to the number of items in listBox4 and listBox5
    {
       string item1 = listBox4.Items[i].ToString();
       string item2 = listBox5.Items[i].ToString();
       createaccount(item1, item2); //call a method using the items at index 'i'
    }
    It's not a bug, it's a feature!

Page 1 of 2 12 LastLast

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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center