Howdy. You don't need to use nested loops to find your clicked "cell."
This assumes that your mapping is in row-major order - i.e. you traverse the
cells horizontally and then move down to the next row. If your mapping is
column-major order, then reverse the formula like this:
The "creeping" error you reported may be due to the fact that the shapes overlap
or that there may be a gap between your shapes. You can correct for this -
assuming that the gap or the overlap is consistent- by using a better width and
height:
> I am using VB6 pro and i have created and array (named wall) of equally
> sized squares using the shape control. I have made quite a lot of them: 2914
> i have made it into a big square that is 54 squares wide by 53 ssquares
> long. I have created cose for the MouseDown event to find which square is
> being clicked on (lots and lots of pictures) the problem is the code only
> works for the top left as it goes to the right or down every six squares it
> gets off by one square. I know this may sound confusing but here is the code
> i have tried to comment as much as i could. basically i have two variables
> one w for width in a for w = 0 to 54 and then one embedded h = 0 to 53 then
> i test it with X,Y and if it works i convert it to i (the number of the
> square in the array wall)
> well here is my code:
> Private Sub Form_MouseDown(Button As Integer, _
> Shift As Integer, X As Single, Y As Single)
> w = 0 ' variable for row
> h = 0 'variable for column
> Dim i 'variable for index number
> For h = 0 To 53 Step 1 'height 0 to 53
> For w = 0 To 54 Step 1 'width 0 to 54
> If CInt(X) >= (w * wall(0).Width) + wall(0).Left And CInt(X)
> <= (w + 1) * wall_(0).Width + wall(0).Left Then 'test for in between x's
> If CInt(Y) >= h * wall(0).Height + wall(0).Top And
> CInt(Y) <= (h + 1) * wall_(0).Height + wall(0).Top Then 'test for in between
> y's
> 'Following if's get the index number of the
> corresponding square
> If w >= 0 And w <= 6 Then i = (54 + 1) * h + w
> If w >= 7 And w <= 13 Then i = (54 + 1) * h + w + 1
> If w >= 14 And w <= 20 Then i = (54 + 1) * h + w + 2
> If w >= 21 And w <= 27 Then i = (54 + 1) * h + w + 3
> If w >= 28 And w <= 34 Then i = (54 + 1) * h + w + 4
> If w >= 35 And w <= 41 Then i = (54 + 1) * h + w + 5
> If w >= 42 And w <= 48 Then i = (54 + 1) * h + w + 6
> If w >= 49 And w <= 55 Then i = (54 + 1) * h + w + 7
> 'Debug.Print i, " ", w
> Debug.Print X, Y
> wall(i).BackStyle = 1 'make the square being clicked on
> black
> End If
> End If
> 'Debug.Print test1, " ", test2
> Next w
> Next h
> 'Debug.Print X, Y
> End Sub
> Thank you in advance,
> Andrew