Results 1 to 10 of 10

Thread: For Loop Problem

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2008
    Posts
    84

    For Loop Problem

    I need some code that goes through each item in a listbox and executes a for next loop for each item

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: For Loop Problem

    What? That makes no sense. Could you try that again?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jul 2008
    Posts
    84

    Re: For Loop Problem

    K sorry

    I have a listbox

    I need to execute a For...Next Loop for each item in that listbox

  4. #4
    Fanatic Member manhit45's Avatar
    Join Date
    May 2009
    Location
    Ha noi - Viet Nam
    Posts
    826

    Re: For Loop Problem

    Quote Originally Posted by Tddupre View Post
    K sorry

    I have a listbox

    I need to execute a For...Next Loop for each item in that listbox
    Code:
    for i as integer = 0 to listbox1.item.count -1
    
    listbox1.items(i)= 'something
    
    next
    --***----------***-----

    If i help you please rate me.

    Working with Excel * Working with String * Working with Database * Working with array *

    K51 ĐH BÁCH KHOA HÀ NỘI - Khoa CNTT pro.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jul 2008
    Posts
    84

    Re: For Loop Problem

    Thx buts thats not it. I guess the correct term would be nested for...loop

    so something like

    for i as integer = 0 to listbox1.items.count -1

    for a as integer = 0 to 12
    msgbox(listbox1.items(i))
    next

    next

    but the actual code will have more to it

  6. #6
    Fanatic Member manhit45's Avatar
    Join Date
    May 2009
    Location
    Ha noi - Viet Nam
    Posts
    826

    Re: For Loop Problem

    Quote Originally Posted by Tddupre View Post
    Thx buts thats not it. I guess the correct term would be nested for...loop

    so something like

    for i as integer = 0 to listbox1.items.count -1

    for a as integer = 0 to 12
    msgbox(listbox1.items(i))
    next

    next

    but the actual code will have more to it
    i write code manually so it is a bit uncorrect. other way :

    Code:
    For Each item In ListBox1.Items
                MsgBox(item)
            Next
    --***----------***-----

    If i help you please rate me.

    Working with Excel * Working with String * Working with Database * Working with array *

    K51 ĐH BÁCH KHOA HÀ NỘI - Khoa CNTT pro.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jul 2008
    Posts
    84

    Re: For Loop Problem

    ok this isnt working lol. I can't get the right words to explain it.

    Let me try one more time:

    What I have right now

    A For...Next loop that is only targeting one item which is in a text box. I want to use a listbox instead of this textbox so the for...next loops can do a lot at the same time. So i need something that will go through the listbox and do this for...next loop for every item in it.

  8. #8
    Addicted Member
    Join Date
    Jul 2009
    Posts
    138

    Re: For Loop Problem

    The for loop that manhit wrote will loop through every item in the list box. Looping through every item again while already looping through them is redundant.

    Code:
    Dim number As Integer = 1
    For Each item In ListBox1.Items
        item.Text = "Item " & number
        number += 1
    Next
    You can do whatever you want to each individual list box item in this for loop.

    If this is not what you need, you will have to explain EXACTLY what you want a little more clearly.
    Last edited by Empyreal; Jul 17th, 2009 at 09:56 PM.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Jul 2008
    Posts
    84

    Re: For Loop Problem

    well here is the loop that doesnt work correctly

    Code:
         
    "this is the newly added for...next  loop
    For Each item In ListBox1.Items
    
                    'Sets server and port for smtp server
                    Dim Smtp As New System.Net.Mail.SmtpClient(SMTP1, Port)
    
                    'Message Details
                    Dim Message = New System.Net.Mail.MailMessage("1234@mail.com", item, subject, MSSG)
    
                    'Enables SS1 Encryption.
                    Smtp.EnableSsl = True
                    Try
                        'Sets username and password for account
                        Smtp.Credentials = New System.Net.NetworkCredential(Email, password)
                    Catch ex As Exception
                        MsgBox(ex.Message)
                    End Try
    
    
    
                    If Me.BackgroundWorker1.CancellationPending Then
                        Exit For
    
                    End If
                    Try
                        For n As Integer = 1 To Number
    
                            Smtp.Send(Message)
                            Me.BackgroundWorker1.ReportProgress(n, "Working...")
                        Next
                    Catch ex As Exception
                        MsgBox(ex.Message)
                    End Try
    
                Next

  10. #10
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,289

    Re: For Loop Problem

    Why do you want to send the same email to the same recipient n times? Are you trying to flood someone's mailbox?
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width