-
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();
}
-
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
-
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.
-
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.
-
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?
-
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?
-
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
-
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.
-
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.
-
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.
-
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?
-
Re: For loop not iterating through a list box
What if I need to loop through both listBox4 and 5?
-
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.
-
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.
-
Re: For loop not iterating through a list box
Quote:
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'
}
-
Re: For loop not iterating through a list box
Hey guys Im in the same position but with another program. Im not planning to sell this one so I can share. It only does what its supposed to do for the first item then just goes down the list not doing it for the rest.
Code:
try
{
for (int i = 0; i < listBox1.Items.Count; i++)
{
if (listBox1.SelectedIndex < listBox1.Items.Count - 1)
{
listBox1.SelectedIndex = listBox1.SelectedIndex + 1;
listBox1.SetSelected(listBox1.SelectedIndex, true);
RefreshIESettings(listBox1.SelectedItem.ToString());
string url = textBox1.Text;
webBrowser1.Navigate(url);
}
}
}
catch (Exception ex)
{
MessageBox.Show (ex.Message);
}
}
-
Re: For loop not iterating through a list box
Once again you have not shown the code which may explain where the problem is nor did you tell us what it is supposed to do but is not doing.
It does appear that you would be calling the navigate method in rapid succession and likely only seeing the result of the last call.
I have no idea where the text in the textbox is coming from nor if it is supposed to change even if it does I would think you would never notice unless you step through the code as it likely is at the last item in the loop before it even bothers to refresh the text shown in the box.
-
Re: For loop not iterating through a list box
Okay its a program that will visit a site with the proxy that the user imports from a text file. The text above is "supposed" to change settings (proxys) and visit the site.
-
Re: For loop not iterating through a list box
First you would need to set a proxy
Ping the website with that proxy
If the ip gets information through that proxy
Then test proxy and manually switch by hitting a button such as try next proxy
There is a program already out there for this task actually several...
What are you trying to accomplish with this?
Antibanning script to look for working proxies for some forum or create a bot to spam a forum ?
I will warn you that with the few clicks of a button it is rather easy to ban your IPs one by one as your bot registers not to mention there are ban lists and many of proxies are already known spam IP's and the ones that are not get added to the listing rather quickly so any spam to boost your rank in google or whatever you are trying to do will easily and quickly be gone there are places dedicated to this 24/7...
Some kind of bruteforce password cracking utility?
Or Anonymous surfing which I highly doubt cause there are many free and open source ways to surf anonymously....
I doubt many will support such a negative piece of software or help you get it working. If you are not able to state its it's reason for being used I would say you are likely posting to deaf ears at this forum while I could be wrong cause I do only speak for myself but it seems that most folks here are only willing to help those that genuinely need support or help.
Enjoy and good luck with your venture if it is a worthy cause.
-
Re: For loop not iterating through a list box
Did someone say something ;)
I had a feeling earlier that this person may be up to no good. Looks like that feeling was correct.
-
Re: For loop not iterating through a list box
No :| Im not doing spamming or anything ilegal. I just wanted to make a fake traffic generator for one of my sites. It will visit the site with the proxy and count as a view for it. Its mostly just me trying to fix the for loop for my other program.
I dont need to worry bout the proxy stuff like testing if its alive or not. I already have the proxy code inserted in my code and RefreshIeSettings just calls it and uses the proxy.
How would I fix the for loop?
-
Re: For loop not iterating through a list box
Quote:
Originally Posted by
kytro360
No :| Im not doing spamming or anything ilegal. I just wanted to make a fake traffic generator for one of my sites. It will visit the site with the proxy and count as a view for it.
The only reason I can think of for doing this is to make it look that your site is more popular than it really is, maybe to fool advertisers. This would count as fraud, which is illegal.
If you have another, valid, reason for faking traffic, could you please share it with us?
-
Re: For loop not iterating through a list box
You guys are overthinking this. Like I said that was just a test. If you dont want to help then dont, Ill ask elsewhere where people dont jump to assumptions :/
-
Re: For loop not iterating through a list box
What do you expect, you don't show the code but we can tell that you are using multiple proxies and creating accounts or at least are trying to. This is not something most of us would or even could within the rules help with.
In any case you keep talking about the loop not working but the problem is not the loop. The loop works fine it is your web code which you have not shown that is not working.
-
Re: For loop not iterating through a list box
What version of .net are you using?
What is the IDE you are using to do your code?
What is the error message you are getting?
There needs to be a better description of the error I dunno weather it is a syntax error or something else...
Anyhow give the error message. If you get too fed up there is always a program called "MagicTrafficBot" uses multiple browser profiles and such to accomplish the same thing but I will tell you one thing ... While this may increase your Alexia rankings google is now hip to all of this so spamming your link or inflating traffic actually lowers your ranking in google. Just something to take into consideration link farming and fake traffic causes the reverse of what you want to happen over time.
-
Re: For loop not iterating through a list box
What version of .net are you using?: The latest
What is the IDE you are using to do your code?: Whats IDE
What is the error message you are getting?: No error messages
-
Re: For loop not iterating through a list box
From the looks of it the navigate statement is the issue or one of them anyway.
Say for example you have 100 items in that list
You will be sending 100 navigate commands to the browser control in a very short period of time possibly milliseconds which means it does not even come close to having enough time to make a connection before it is told to make a different connection.
Unless of course the code is stopping and waiting for the connection each time.
-
Re: For loop not iterating through a list box
How about if I make my program sleep? Or should I incorporate a timer? But the thing about a timer is that if the page loads somehow slower then the set amount it might not work correctly.