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

Looping through Listbox

Discussion in 'Visual Basic .NET' started by fpforum, Dec 31, 2012.

  1. fpforum

    fpforum Junior Member

    Joined:
    Apr 22, 2008
    Messages:
    117
    Likes Received:
    4
    Home Page:
    Hey everyone..Hopefully someone can shed some light onto where I'm going wrong at here.

    I'm writing a simple program that will test aol/aim accounts..The program should take the first account in the listbox..break it up into username:password format, and then try to login.

    Everything seams to be working properly with the httpwebrequest but the problem I'm having is with the loop. Ideally, if the account has an unsuccessful login then it will remove the item from the listbox and then move onto the next (or start over once it reaches the end of the list)..I put a dummy login into the listbox first and tried it out. Just as expected I get the msgbox stating the login was unsuccessful - but the item is never removed from the listbox and it never actually starts over with the next item in the list. Instead, I will get one msgbox after another stating that the login was unsuccessful..

    Here is my code
    Code:
    Private Sub btnLogin_Click(sender As System.Object, e As System.EventArgs) Handles btnLogin.Click
    
            Dim x As Integer = lstAccts.Items.Count - 1
    
    
            Do While x > 0 'For looping through account list
    
                lstAccts.SetSelected(0, True)
    
                Dim line As String
                Dim parts() As String
    
                line = lstAccts.SelectedItem
                parts = line.Split(":")
    
                Dim Username As String = parts(0) 'The Aol screen name
                Dim Password As String = parts(1) 'The Aol password
                Dim Post As String = "/_cqr/login/login.psp sitedomain=startpage.aol.com&siteId=&lang=en&locale=us&authLev=0&siteState=OrigUrl%253Dhttp%25253A%25252F%25252Fwww.aol.com%25252F&isSiteStateEncoded=true&mcState=initialized&uitype=std&use_aam=0&offerId=&seamless=y&regPromoCode=&usrd=1795702&doSSL=&redirType=&xchk=false&tab=&lsoDP=id%3DE972048F-57EC-62F4-2F30-2E5009CA0C6C&loginId={0}&password={1}"
                Post = String.Format(Post, Username, Password) 'Password(I))
                Dim InformationA As String
                Dim InformationB As String
                Cookies = New CookieContainer()
                Dim Encoding As New ASCIIEncoding()
                Dim Data As Byte() = Encoding.GetBytes(Post)
                Dim Request As HttpWebRequest = DirectCast(WebRequest.Create("https://my.screenname.aol.com/_cqr/login/login.psp?sitedomain=startpage.aol.com&siteState=OrigUrl%3dhttp%253A%252F%252Fwww.aol.com%252F&authLev=0&lang=en&locale=us"), HttpWebRequest)
                Request.Method = "POST"
                Request.UserAgent = ".NET Example Client"
                Request.ContentType = "application/x-www-form-urlencoded"
                Request.CookieContainer = Cookies
                Dim DataStream As Stream = Request.GetRequestStream()
                DataStream.Write(Data, 0, Data.Length)
                Dim Response As HttpWebResponse = DirectCast(Request.GetResponse(), HttpWebResponse)
                Dim SR As New StreamReader(Response.GetResponseStream())
                InformationA = SR.ReadToEnd()
                InformationB = InformationA
                If InformationB.Contains("Username Service") Then
                    MessageBox.Show("Logged In!")
                    Exit Do
                    'PERFORM NEXT STEP
                ElseIf InformationB.Contains("Incorrect Username") Then
                    lstAccts.Items.Remove(x)
                    MsgBox("Incorrect Username!")
                End If
            Loop
    
    
        End Sub
    
     
  2. gimme4free

    gimme4free Executive VIP Jr. VIP Premium Member

    Joined:
    Oct 22, 2008
    Messages:
    1,879
    Likes Received:
    1,931
    lstAccts.Items.RemoveAt(x) instead of lstAccts.Items.Remove(x)

    Also remember that then the Listbox will have 1 less value so removing at x will not remove that time.

    Alternatively you can remove by the string i think as x is an integer so listAccts.Items.Remove(line)