# Thread: How to change to next row

1. ## How to change to next row

I have a picture that contains 35 colored shapes.

I want to print shapes from 1 thru 7 on row 1 of the target picture then drop down to the next row and print shapes 8 thru 14 on row 2 of the target, then shapes 15 thru 21 on row 3 etc, etc

Counter is just a counter counting each shape through the For.....Next loop

If I use If Counter Mod 8 = 0 to change to the next row it will work for rows 1 and 2 but then after that it wont work for rows 3 etc.

What do I use to change after every 7 shapes to go to the next row

Code:
```Private Sub Command1_Click()
Static XPos As Integer
Static YPos As Integer
Static Counter As Integer
Static VCounter As Integer

Counter = Counter + 1

If Counter > 35 Then Exit Sub

If Counter Mod 8 Then
VCounter = VCounter + 1

YPos = YPos + (50 * VCounter)

XPos = 0
End If

Picture2.ForeColor = vbWhite

For x = 0 To Picture2.ScaleWidth
For y = 0 To Picture2.ScaleHeight
If Picture2.Point(x, y) = MaskColor Then
picPiece.PSet (x + XPos, YPos + y), Picture3.Point(x, y)
Else
Picture2.PSet (x, y)
End If
Next y
Next x
End Sub```

2. ## Re: How to change to next row

Originally Posted by Code Dummy
...
Counter is just a counter counting each shape through the For.....Next loop
...
Code:
```Private Sub Command1_Click()
Static XPos As Integer
Static YPos As Integer
Static Counter As Integer
Static VCounter As Integer

Counter = Counter + 1

If Counter > 35 Then Exit Sub

If Counter Mod 8 Then
VCounter = VCounter + 1

YPos = YPos + (50 * VCounter)

XPos = 0
End If

Picture2.ForeColor = vbWhite

For x = 0 To Picture2.ScaleWidth
For y = 0 To Picture2.ScaleHeight
If Picture2.Point(x, y) = MaskColor Then
picPiece.PSet (x + XPos, YPos + y), Picture3.Point(x, y)
Else
Picture2.PSet (x, y)
End If
Next y
Next x
End Sub```
You say Counter is counting the pieces in the For....Next loop, but it isn't inside a For...Next loop, so is the For...Next loop you're referring to outside the Sub, calling the click event repeatedly for each piece?

Shouldn't the statement

If Counter Mod 8 Then

be

If (Counter Mod 8) = 0 Then

Of course, if you want every 7 pieces to be on a row, then it should be Mod 7, not Mod 8.
And the Counter should start with 0 for the first piece, not 1.
If you want Counter to start at 1, then use ((Counter - 1) Mod 7)

Also, you have Counter declared locally, so the command button would only work one time through the series of 35 pieces since you have no way to reset the Counter back to 0.

I would have to assume that perhaps the command button is just a tempoary test, to allow you to place one piece at a time, with each click.

I can't figure out what the lower nested loop represents.
It looks like picture2 should be the Mask image for one piece, and picture3 is the image for one piece.
It looks like picPiece is suppose to be a much larger bitmap where you plan on drawing the pieces in rows and columns.

If the above is not your intention, then perhaps you have some logic backwards. You mention having a picturebox with 35 shapes in it.
Which picturebox is that?
How are the 35 shapes laid out?
Your code doesn't look like it is trying to access a subsection of a source bitmap to access a shape, and copy it elsewhere.
Code:
```     If Picture2.Point(x, y) = MaskColor Then
picPiece.PSet (x + XPos, YPos + y), Picture3.Point(x, y)
Else
Picture2.PSet (x, y)
End If```
I'm not sure why you have the Else statement there, which will modify picture2, setting the pixels that are not the MaskColor to the current Foreground color.

3. ## Re: How to change to next row

First, Counter is not in a For....Next loop as I said (that was an error on my part) but it is a Static variable inside the button click which I click on for each shape so I am doing one shape per click. Picture2 is the picturebox that has the 35 colored shapes. These are the shapes of the pieces of the puzzle and they are bound together as one picture, separated only by their different colors. These shapes are laid out in a 5 x 7 matrix (5 rows, 7 columns). Picture2 is the same exact size as Picture3 which is the original art work for the puzzle. Each pixel on Picture2 then corresponds with the same pixels on Picture3. One time in the sub does one shape. Before I click on the button again I have to click on a radio button which changes to the next mask color. The Else is not used anymore because I was clearing out the mask each time but reloaded it prior to clicking on the button. Now I just leave it alone so I don't need to reload the mask picture every time I do a shape. The output of this code draws the original image per shape onto picPiece but laid out as separate pieces with white space between each one in the 5 x 7 matrix. picPiece is larger that Picture2 and 3 as it needs the extra room for the separate pieces. In another step in the program these pieces are cut out and made into separate images which will later be loaded into user controls and this will be the puzzle to put together. Setting Counter to 0 and not 1 then using If ((Counter) Mod 7) = 0 Then does what I need. In the puzzle game I will have the code to do the "gluing" of the pieces together. As of right now I am just experimenting around so not all of the logic is being applied since I am doing a little bit at a time but later it will be basically automatic and I wont be doing one shape at a time; it will do the entire puzzle from start to finish with just a single button click

4. ## Re: How to change to next row

If you have 7 items in a row, you cannot use Mod 8. Another way to reiterate what Passel is saying...

1st time through, Counter=1, so 1 Mod 8 = 7 failure, but Statics are zero, so it accidentally works
2nd row begins, Counter=8, so 8 Mod 8 = 0 works and no problems
3rd row begins, Counter=15, so 15 Mod 8 = 7 failure
4th row begins, Counter=22, so 22 Mod 8 = 6 failure
5th row begins, Counter=29, so 29 Mod 8 = 5 failure

Use Mod 7 = 0 after the loops, not before. That way, after each full row is processed: yPos is updated, xPos reset to zero for the next row and Counter is a nice multiple of 7:
after 1st row: 7, 2nd row: 14, 3rd row: 21, 4th row: 28, 5th row: 35
Code:
```Counter = Counter + 1
If Counter Mod 7 = 0 Then
If Counter = 35 Then
... reset all static variables, including Counter
Else
... set yPos, reset xPos for next row
... tip: don't need VCounter. Next row's index at this point is: Counter\7
End If
Else
... set xPos for next column
End If```
That may solve your problem. I also don't see where xPos is being changed, but you may simply have not posted the entire routine.

FYI: Personally, I don't like using Statics inside button clicks unless you have a way to reset them. Otherwise, when compiled, the only way they can be reset to zero (for a new game), is to unload the form, set it to nothing, then reload it or simply close & restarting the application. In your case, you can reset them at the end of the routine when Counter = 35 (last row is processed)

#### Posting Permissions

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

Featured