using for loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • navda
    New Member
    • Jan 2010
    • 9

    using for loop

    hi i am a new user to visual basic which i find very interesting but one small problem stuck out behind me using for loop
    i get confused with the following code please help at(nvnrai8@gmai l.com)

    private sub command1_click( )
    for n=0 tolist1.listcou nt-1
    if list1.selected( n) =true then
    list2.additem list1.list(n)
    list1.selected( n)=false
    end if
    next n
    end sub

    what does next n work here and where it goes while working and also how for loops works here.this is a program which adds list1 items to list2 box

    if anyone has the solution please respond back....
  • vb5prgrmr
    Recognized Expert Contributor
    • Oct 2009
    • 305

    #2
    n, in this case is a variable that holds the incremented value from start to finish...

    For n = startvalue to finishvalue

    For n = 0 to list1.listcount - 1

    next n 'here is where n = n + 1

    A for loop is normally used to walk through a list or collection of items that are sequentially numbered, either forward or backward, but it does have a step option that allows you to change that behavior...

    for example, your code could be modified to walk backwards from listcount - 1 to 0 by the use of the step keyword and a minus 1 value...
    Code:
    For n = List1.ListCount -1 To 0 Step - 1
      '...
    Next n
    Or if the occasion arises, you can increment by two or three or whatever by use the the step keyword and a value...
    Code:
    For n = 10 To 100 Step 10
      '...
    Next n
    or backwards...
    Code:
    For n = 100 To 10 Step - 10
      '...
    Next n


    Good Luck

    Comment

    • navda
      New Member
      • Jan 2010
      • 9

      #3
      hi thanks a lot for answering me back....
      well i still have some doubts with previous solution

      private sub command1_click( )
      for n=0 tolist1.listcou nt-1
      if list1.selected( n) =true then
      list2.additem list1.list(n)
      list1.selected( n)=false
      end if
      next n
      end sub

      ok i got the point about the incrementation of next n=n+1.but i want to know that
      if n=0 means first index value of my list item and listcount means end value of my list item let it be 7 i.e, 6 by (list1.listcoun t-1)

      well what does list1.selected( n)=true means here.... is it saying that the index value between 0 to 6 is correct, or a condition which checks that i have selected the index value between 0 to 6....please make me clear

      as well i want to know if i select the items from list 1 having index value between 0 to 6 let it be index 3 and send it to list2.... then next n takes me to index 4 then index 5 then index 6(who does not respond and display any item name or send its value to list 2 because i have not selected those items)??? and if my loop condition ends the program ends and in the code as said (list1.selected (n)=false) makes my selected item false in list 1.

      so what happens if i select the same item next time how does for loop react does it makes again the value of n=0 and start from the begining?will there be any flow of previous loop in next selection....?? ?

      is this the concept here of for loop...please explain me....



      thanks looking forward...at(nv nrai8@gmail.com )

      Comment

      • vb5prgrmr
        Recognized Expert Contributor
        • Oct 2009
        • 305

        #4
        VB6.0 can be confusing when it comes to this because the counts of a lot of controls are one (1) based, meaning they start counting at 1, while references in control to the actual elements of the control are zero (0) based...

        ?Selected is the item in the control selected?...



        Good Luck

        Comment

        • Guido Geurs
          Recognized Expert Contributor
          • Oct 2009
          • 767

          #5
          dear,

          attached is an example explaning how lists works.

          hope this will help.

          br,
          Attached Files

          Comment

          Working...