-
September 12th, 2011, 06:07 PM
#1
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();
}
-
September 12th, 2011, 07:50 PM
#2
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.
-
September 12th, 2011, 07:56 PM
#3
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.
-
September 12th, 2011, 08:09 PM
#4
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.
-
September 12th, 2011, 08:16 PM
#5
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?
-
September 12th, 2011, 10:03 PM
#6
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.
-
September 13th, 2011, 06:13 AM
#7
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
-
September 13th, 2011, 07:11 AM
#8
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.
-
September 13th, 2011, 12:28 PM
#9
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.
-
September 13th, 2011, 04:03 PM
#10
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.
-
September 14th, 2011, 02:49 AM
#11
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!
-
September 14th, 2011, 03:51 PM
#12
Re: For loop not iterating through a list box
What if I need to loop through both listBox4 and 5?
-
September 16th, 2011, 02:37 AM
#13
Re: For loop not iterating through a list box
Originally Posted by kytro360
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!
-
September 14th, 2011, 04:12 PM
#14
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.
-
September 14th, 2011, 04:38 PM
#15
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.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|