C#: How to cycle through listbox?

Status
Not open for further replies.

kytro360

Member
Original poster
member
Joined
Sep 9, 2009
Posts
376
Reaction score
6
Points
75
I am working with webrequests and am making a program that registers on a site. Basically I have a listbox that has a bunch of emails for the registration process. Basically the user will put how many emails they want (which basically means how many accounts they want) and how do it make it so when my program is going through the webrequests that on each one it does it will go down the list of the listbox picking a new email until the end?
 

scriptskat

Member
member
Joined
Aug 31, 2011
Posts
139
Reaction score
22
Points
140
I had this problem as well, the way I went about it is less of a solution and more of a workaround, I copied the content of the list box (in real time) into an array then I got some code to set an item in the list box as selected then I retrieve the selected index in the array and got a nice string value... But I wanted the values to come out at random, the code looked like this:
Code:
                                // Get random link from list
                                int i = random.Next(linksCount - 1);
                                linksList.SetSelected(linksList.SelectedIndex, false);
                                linksList.SetSelected(i, true);
                                link = _links[linksList.SelectedIndex];

I left out declarations, anyway this is probably not the most efficiant way to do things, but it worked for me. Hope this helps.
 
  • Like
Reactions: kytro360

Flip

Old School
old school
member
Joined
Jul 23, 2009
Posts
263
Reaction score
69
Points
108
This is easier (IMO) with a richTextBox since it already has array control so you can just do something like this

Code:
richTextBox1.Lines[i].ToString()
Although doing it with a listBox didn't turn out to be that hard either.

YF6SE.png


Of course you don't want to output it to a richTextBox, but that was just to show you it works.
 

kytro360

Member
Original poster
member
Joined
Sep 9, 2009
Posts
376
Reaction score
6
Points
75
@scriptscat What other derivitatives do I need. I only figured out: Random random = new Random();
 

scriptskat

Member
member
Joined
Aug 31, 2011
Posts
139
Reaction score
22
Points
140
@scriptscat What other derivitatives do I need. I only figured out: Random random = new Random();

Yea thats right,

linksList is the actual list box item.

link would be your final output of an email that you would use

_links is an array and when you add a new item to your list box you will need to add it to _links in the same order.
Also I defined the array at the beginning of the namespace so it works across functions.

Also I defined the array at the beginning of the namespace so it works across functions.

Overall I should have changed links to emails but this is from some code I'm currently working on so I was lazy
 

kytro360

Member
Original poster
member
Joined
Sep 9, 2009
Posts
376
Reaction score
6
Points
75
@scriptscat What would linksCount be? You never mentioned that. Also how would I call this in my thread.

This is the code to call my webrequest: signup (ENTER WHAT YOU WANT TO FILL IN TO THE SITE HERE)

@Flip I tried your code but it didnt work, and it didnt show up on the site in the webrequest:

(P.S: The code below calls the webrequest and fills in the details found on each of the items into the site)

Code:
    for (int i = 0; i < numericUpDown2.Value; i++)
         {

             listBox4.SetSelected(i, true);


         }
           signup(txtBoxImportNames1.Text + txtBoxImportNames2.Text, listBox4.SelectedItem.ToString(), txtBoxImportPasswords.Text, txtBoxUsernames.Text);
      

                  
        }
 

Flip

Old School
old school
member
Joined
Jul 23, 2009
Posts
263
Reaction score
69
Points
108
Code:
signup(txtBoxImportNames1.Text + txtBoxImportNames2.Text, listBox4.SelectedItem.ToString(), txtBoxImportPasswords.Text, txtBoxUsernames.Text);

This needs to go inside the for loop.

Each iteration of your current for loop is selecting the next item in the listBox and doing nothing with it.
 

kytro360

Member
Original poster
member
Joined
Sep 9, 2009
Posts
376
Reaction score
6
Points
75
@Flip Its not posting the email to the form on the site while all the other fields are except the email one.

I did some tests and I think there might be something wrong with my code because as a test I took it out of the for loop and instead of calling listBox4.SelectedItem.ToString() I wrote "Hi" to test if it would appear on the form. I loaded the document text in a richtextbox and loaded it in the webbrowser and every single form got filled except the email one.

Can anyone help? i copied the field and id exactly from the site.
 

Flip

Old School
old school
member
Joined
Jul 23, 2009
Posts
263
Reaction score
69
Points
108
@Flip Its not posting the email to the form on the site while all the other fields are except the email one.

I did some tests and I think there might be something wrong with my code because as a test I took it out of the for loop and instead of calling listBox4.SelectedItem.ToString() I wrote "Hi" to test if it would appear on the form. I loaded the document text in a richtextbox and loaded it in the webbrowser and every single form got filled except the email one.

Can anyone help? i copied the field and id exactly from the site.

Post your code along with the HTML code for the email field.
 

kytro360

Member
Original poster
member
Joined
Sep 9, 2009
Posts
376
Reaction score
6
Points
75
This is the actual webrequest for this page: http://www.twitter.com/signup

Code:
 public void signup(string name, string email, string password, string username)
        {

            try
            {
                System.Net.ServicePointManager.Expect100Continue = false;

                string postData = "user[name]=" + name + "&user[email]" + email + "&user[user_password]=" + password + "&user[screen_name]=" + username + "";


                System.Net.ServicePointManager.Expect100Continue = false;

                UTF8Encoding encoding = new UTF8Encoding();
                byte[] byteData = encoding.GetBytes(postData);

                HttpWebRequest postReq = (HttpWebRequest)WebRequest.Create("https://twitter.com/signup");
                postReq.Method = "POST";
                postReq.KeepAlive = true;
                postReq.ContentType = "application/x-www-form-urlencoded";
                postReq.Referer = "Referer: https://twitter.com/signup";
                postReq.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6";
                postReq.ContentLength = byteData.Length;


                Stream postreqstream = postReq.GetRequestStream();
                postreqstream.Write(byteData, 0, byteData.Length);
                postreqstream.Close();
                HttpWebResponse postresponse = default(HttpWebResponse);

                postresponse = (HttpWebResponse)postReq.GetResponse();
                StreamReader postreqreader = new StreamReader(postresponse.GetResponseStream());
                StreamReader reader = new StreamReader(postresponse.GetResponseStream());
                string theusercp = reader.ReadToEnd();
                richTextBox1.Text = theusercp;
                webBrowser1.DocumentText = theusercp;
               
            }

            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);

            }



        }

and you call it like this: signup ( );
 

Flip

Old School
old school
member
Joined
Jul 23, 2009
Posts
263
Reaction score
69
Points
108
I don't really know how postdata works, so maybe someone else can help.
 

kytro360

Member
Original poster
member
Joined
Sep 9, 2009
Posts
376
Reaction score
6
Points
75
No worries, I asked on another forum and they said I forgot an = sign in my code.

Gonna test the code for your listbox to cycle through

Okay your code for the listbox cycles through all the items but when it comes to the actual webrequest it goes down the last then when it goes down the list to the final item thats the one it uses for the webrequest and it doesnt go through all of the other items just that one then stops.

@Flip As a test I added three items in the listbox. Your code goes to the first one and as far as Im aware does the webrequest then it goes to the second then immediately goes to the third one. How do I make it wait for each one to complete before moving on?
 

scriptskat

Member
member
Joined
Aug 31, 2011
Posts
139
Reaction score
22
Points
140
linksCount is list.Items.Count();

Sorry >.>
 

kytro360

Member
Original poster
member
Joined
Sep 9, 2009
Posts
376
Reaction score
6
Points
75
@scriptscat I tried adding your code but got a bunch of errors. I dont understand, are there any parts missing or...?
 

Flip

Old School
old school
member
Joined
Jul 23, 2009
Posts
263
Reaction score
69
Points
108
Code:
signup(txtBoxImportNames1.Text + txtBoxImportNames2.Text, listBox4.SelectedItem.ToString(), txtBoxImportPasswords.Text, txtBoxUsernames.Text);
This needs to go inside the for loop.

Each iteration of your current for loop is selecting the next item in the listBox and doing nothing with it.

No worries, I asked on another forum and they said I forgot an = sign in my code.

Gonna test the code for your listbox to cycle through

Okay your code for the listbox cycles through all the items but when it comes to the actual webrequest it goes down the last then when it goes down the list to the final item thats the one it uses for the webrequest and it doesnt go through all of the other items just that one then stops.

@Flip As a test I added three items in the listbox. Your code goes to the first one and as far as Im aware does the webrequest then it goes to the second then immediately goes to the third one. How do I make it wait for each one to complete before moving on?

I believe I answered that question with my previous post.
 

kytro360

Member
Original poster
member
Joined
Sep 9, 2009
Posts
376
Reaction score
6
Points
75
@Flip I already put the code in the for loop
 
Status
Not open for further replies.