okay. i ahve a question, if any one is willing to help me out.

aim: recreate a dynamic 'lights out'

i want to position controls into a grid like view at runtime, so the result would look something like this:

00 01 02 03 04
05 06 07 08 09
10 11 12 13 14
15 16 17 18 19
20 21 22 23 24

now, i thought i could do this with a nested loop statement:

dim i, j, k
i=0 'row
j=0 'column
k=0 'row +column = item number
for i=0 to 4
  for j=0 to 4
    k=i+j
    label(k).left = j * 250 '*250 for horizontal spacing from other controls
    label(k).top = i * 250 '*250 for vertical spacing from other controls
    label(k).captio = k
  next j
next i

so, everytime k was read, it would = 0+0, 0+1, 0+2, 0+3, 0+4, 1+0, 1+1, etc

But, after running that, i only ever end up with something like:

(xx means nothing was here)

00 01 02 03 04
xx xx xx xx 05
xx xx xx xx 06
xx xx xx xx 07
xx xx xx xx 08

or (k=i*j)

00 xx xx xx xx
xx 01 xx xx xx
xx xx 02 xx xx
xx xx xx 03 xx
xx xx xx xx 04
etc

now im stumped.
am i going about this wrong?

thanks in advance.

If you use
i + j
In the first iteration of i
i = 0 and j = 0 , 1 ,2 ,3 ,4
so k = i + j = 0, 1, 2, 3, 4
It is correct

In the second Iteration of i
i = 1 and j = 0, 1, 2, 3, 4
so k = i + j = 1, 2 ,3 ,4 ,5

and so on

if you use k = (i * no of columns) + j
then
i=0 & k = 0, 1, 2 ,3 ,4 in the first iteration
i=1 & k = 5, 6, 7, 8, 9 in the second iteration
and so on

Here No of Columns is 5

Your Corrected code is

Dim i As Integer, j As Integer, k As Integer
i = 0 'row
j = 0 'column
k = 0 'row +column = item number
For i = 0 To 4
For j = 0 To 4
k = (i * 5) + j ' 5 Refers No of columns
Label1(k).Left = j * 250 '*250 for horizontal spacing from other controls
Label1(k).Top = i * 250 '*250 for vertical spacing from other controls
Label1(k).Caption = k
Next j
Next i

This article has been dead for over six months. Start a new discussion instead.