Results 1 to 20 of 20

Thread: [RESOLVED] How to set up the proper For....Next loop

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Resolved [RESOLVED] How to set up the proper For....Next loop

    I want to arrange a set of 52 cards in 4 rows of 13 cards each

    Card #1 is positioned at Left = 3, Top = 5

    1st row cards 1 to 13
    2nd row cards 14 to 26
    3rd row cards 27 to 39
    4th row cards 40 to 52

    Each card is positioned 2 pixels to the right of the card to it's left and
    each row is positioned 2 pixels below the card above it.
    Last edited by jmsrickland; Jun 15th, 2011 at 12:47 PM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  2. #2
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: How to set up the proper For....Next loop

    What do you have so far? What's the problem with it?

  3. #3
    Addicted Member
    Join Date
    Mar 2006
    Posts
    186

    Re: How to set up the proper For....Next loop

    Here is more or less explained how to do loops in VB6: http://www.vb6.us/tutorials/understa...nd-while-loops

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: How to set up the proper For....Next loop

    What do you have so far? What's the problem with it?

    I have no idea at all how to do a nested For...Next loop

    Code:
     For n1 = 1 To 4     'rows 
       For n2 = 1 To 13 'cards per row
         ???????? ' have no idea whatsoever
       Next n2
     Next n1


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: How to set up the proper For....Next loop

    You say you have no idea how to do a nested loop, but have shown a perfectly valid example... All you need to do now is put code in the middle to set the positions of items based on the loop variables (so the X position based on n2, and the Y position based on n1), exactly as you would with a single loop.


    From the explanation you gave in post #1, the X position would be something like this:
    Code:
    Card((n1-1)*13+(n2-1)).X = 3 + (n2-1) * (CardWidth + 2)
    As is often the case, if you start your arrays and loops at 0 (so n1 = 0 to 3, etc) rather than 1, the code becomes simpler:
    Code:
    Card(n1*13+n2).X = 3 + n2 * (CardWidth + 2)

  6. #6
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: How to set up the proper For....Next loop

    JMS

    Maybe something like this:

    Code:
    nn = 0                                ' counter for card to position
    ht = 10                               ' card height; change as req'd
    For n1 = 1 To 4                       ' rows 
        ntop = 5 + (n1 - 1) * ht + 2      ' sets row at 5 + ht + 2
        nleft = 3                         ' default left for each row
        For n2 = 1 To 13                  ' cards per row
            nleft = nleft + (n2 - 1) * 2  ' increments left by 2 for each card in row
            nn =  nn + 1
            MyCard.Top = ntop
            MyCard.Left = nleft
        Next n2
    Next n1
    Spoo

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: How to set up the proper For....Next loop

    Post #5

    That causes all the cards of each row to be positioned too far to the right. The first card of each row is positioned 55 but it has to be 3
    Last edited by jmsrickland; Jun 15th, 2011 at 03:33 PM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: How to set up the proper For....Next loop

    OK, here's a problem. There are 52 cards (Image1(1) to Image1(52)) and each card has an index of 1 to 52 so there needs to be a way to address the index of each card.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  9. #9
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: How to set up the proper For....Next loop

    JMS

    My bad .. should read as this

    Code:
    nn = 0                                ' counter for card to position
    ht = 10                               ' card height; change as req'd
    For n1 = 1 To 4                       ' rows 
        ntop = 5 + (n1 - 1) * ht + 2      ' sets row at 5 + ht + 2
        For n2 = 1 To 13                  ' cards per row
            nleft = 3 + (n2 - 1) * 2      ' increments left by 2 for each card in row
            nn =  nn + 1
            MyCard(nn).Top = ntop       ' added index number
            MyCard(nn).Left = nleft     ' added index number
        Next n2
    Next n1
    I eliminated a line of code, and corrected a line of code.
    Hope that works better.

    EDIT:
    I saw your recent post.
    I had things set up for an array, but forgot to add it to the cards themselves.
    I've now done this. Substitute the name as required

    Spoo
    Last edited by Spoo; Jun 15th, 2011 at 03:42 PM.

  10. #10

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: How to set up the proper For....Next loop

    Post #6 and #9

    Nope. That causes the cards to overlap in a very tight row.

    The cards need to be layed out like this:

    Code:
    +---+ +---+ +---+
    |   | |   | |   |
    |   | |   | |   |-------> to card 13
    +---+ +---+ +---+
    +---+ +---+ +---+
    |   | |   | |   |
    |   | |   | |   |-------> to card 26
    +---+ +---+ +---+
    +---+ +---+ +---+
    |   | |   | |   |
    |   | |   | |   |-------> to card 39
    +---+ +---+ +---+
    +---+ +---+ +---+
    |   | |   | |   |
    |   | |   | |   |-------> to card 52
    +---+ +---+ +---+
    P.S. It looks like it's a little more difficult than what we all think.
    Last edited by jmsrickland; Jun 15th, 2011 at 04:05 PM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  11. #11
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: How to set up the proper For....Next loop

    Quote Originally Posted by jmsrickland View Post
    Post #5

    That causes all the cards of each row to be positioned too far to the right. The first card of each row is positioned 55 but it has to be 3
    The 0 based version is fine, so I presume you are still using 1 based, in which case there was a minor omission:
    Code:
    Card((n1-1)*13+(n2-1) + 1).X = 3 + (n2-1) * (CardWidth + 2)
    Of course using a separate variable for the card number (such as spoo's nn) is a valid alternative.

  12. #12
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: How to set up the proper For....Next loop

    JMS

    Sorry again, I misunderstood your requirements.

    I thought you wanted the cards in a given row to be "stacked",
    like in Solitaire (except horizontally, not vertically)

    I now understand you to want cards in a row to each be
    visible, and separated by 2 pixels.

    The code below should cover that
    Code:
    nn = 0                                ' counter for card to position
    ht = 10                               ' card height; change as req'd
    wd = 4                                ' card width; change as req'd
    For n1 = 1 To 4                       ' rows 
        ntop = 5 + (n1 - 1) * ht + 2      ' sets row at 5 + ht + 2
        For n2 = 1 To 13                  ' cards per row
            nleft = 3 + (n2 - 1) * wd + 2 ' set left to 2 beyond right edge of prev card
            nn =  nn + 1
            MyCard(nn).Top = ntop       ' added index number
            MyCard(nn).Left = nleft     ' added index number
        Next n2
    Next n1
    Changes shown in red
    Am I getting closer?

    Spoo

  13. #13
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: How to set up the proper For....Next loop

    Si

    Your approach ..

    Code:
    Card((n1-1)*13+(n2-1) + 1).X = 3 + (n2-1) * (CardWidth + 2)
    .. is clearly more elegant and concise than mine is.

    My brute force approach was developed mainly to separate
    the loop construct issue from the Card index issue, given that
    JMS mentioned he was perplexed by nested loops.

    EDIT:
    Further, I see two additonal errors ...
    Code:
    nn = 0                                ' counter for card to position
    ht = 10                               ' card height; change as req'd
    wd = 4                                ' card width; change as req'd
    For n1 = 1 To 4                       ' rows 
        ntop = 5 + (n1 - 1) * (ht + 2)      ' sets row at 5 + ht + 2
        For n2 = 1 To 13                  ' cards per row
            nleft = 3 + (n2 - 1) * (wd + 2) ' set left to 2 beyond right edge of prev card
            nn =  nn + 1
            MyCard(nn).Top = ntop       ' added index number
            MyCard(nn).Left = nleft     ' added index number
        Next n2
    Next n1
    JMS -- note the correction, parentheses added
    Sorry for the protracted effort here.

    Spoo
    Last edited by Spoo; Jun 15th, 2011 at 04:18 PM.

  14. #14

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: How to set up the proper For....Next loop

    Am I getting closer?

    Not even


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  15. #15

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: How to set up the proper For....Next loop

    Post 11

    Nope, causes an error control array 53 doesn't exisr


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  16. #16

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: How to set up the proper For....Next loop

    This makes a total mess + error

    Code:
       For n1 = 0 To 3 'row
         For n2 = 1 To 13 'cards per row
           Image1(n1 * 13 + n2 + 1).Left = 3 + n2 * (Image1(1).Width + 2)
           Image1(n1 * 13 + n2 + 1).Top = 5 + n2 * (Image1(1).Height + 2)
         Next n2
       Next n1


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  17. #17

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: How to set up the proper For....Next loop

    Post 13 Perfect after I change the height and width

    Code:
    nn = 0                                ' counter for card to position
    ht = 10                               ' card height; change as req'd
    wd = 4                                ' card width; change as req'd
    For n1 = 1 To 4                       ' rows 
        ntop = 5 + (n1 - 1) * (ht + 2)      ' sets row at 5 + ht + 2
        For n2 = 1 To 13                  ' cards per row
            nleft = 3 + (n2 - 1) * (wd + 2) ' set left to 2 beyond right edge of prev card
            nn =  nn + 1
            MyCard(nn).Top = ntop       ' added index number
            MyCard(nn).Left = nleft     ' added index number
        Next n2
    Next n1


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  18. #18
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: How to set up the proper For....Next loop

    Quote Originally Posted by jmsrickland View Post
    Am I getting closer?

    Not even
    JMS

    Odd.. I just tested it.
    Works for me

    EDIT:
    Aha .. seems from your post # 17 that we have a go.

    Spoo
    Last edited by Spoo; Jun 15th, 2011 at 04:36 PM.

  19. #19
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: How to set up the proper For....Next loop

    Quote Originally Posted by jmsrickland View Post
    Post 11

    Nope, causes an error control array 53 doesn't exisr
    That's strange, because I just pasted in into VB (with minor alterations so it displays the numbers) and it doesn't produce the number 53: it goes from 1 to 52
    Code:
    Dim n1, n2
     For n1 = 1 To 4     'rows
       For n2 = 1 To 13 'cards per row
        Debug.Print (n1 - 1) * 13 + (n2 - 1) + 1; " = "; 3 + (n2 - 1) * (50 + 2)
       Next n2
     Next n1
    Quote Originally Posted by jmsrickland View Post
    This makes a total mess + error

    Code:
       For n1 = 0 To 3 'row
         For n2 = 1 To 13 'cards per row
           Image1(n1 * 13 + n2 + 1).Left = 3 + n2 * (Image1(1).Width + 2)
           Image1(n1 * 13 + n2 + 1).Top = 5 + n2 * (Image1(1).Height + 2)
         Next n2
       Next n1
    That is expected, because while you have added +1 to the card index to cope with your 1-based array correctly, and altered the For n1 line correctly, you have not altered the For n2 line (it should be 0 to 12).


    Quote Originally Posted by Spoo View Post
    My brute force approach was developed mainly to separate
    the loop construct issue from the Card index issue, given that
    JMS mentioned he was perplexed by nested loops.
    And that was a good idea... it is also likely to be more efficient

  20. #20

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: How to set up the proper For....Next loop

    Thanks all. I don't think I could even have come close to figuring it out.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

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