Stack Exchange
Calculating matrix for positioning controls
I have to create 39 labels dynamically and place it to Panel1 in form of matrix of 3 columns and 12 rows, like this:
Label1  Label2  Label3
Label4  Label5  Label6
etc... to...
Label37 Label38 Label39

For doing this I have to calculate relative position of each label in "grid" from index.
This is my code:
Dim posX As Integer = 0
Dim posY As Integer = 0
Const offset As Integer = 25

Dim labels(39) As Label
For t As Integer = 1 To 39
    labels(t) = New Label()
    labels(t).Name = "Label" + t.ToString
    labels(t).Text = t.ToString
    labels(t).ForeColor = Color.Red
    labels(t).BorderStyle = BorderStyle.FixedSingle
    labels(t).AutoSize = True

    posX = t Mod 3
    If posX = 0 Then posX = 3

    posY = CInt((t / 3) Mod 12)

    labels(t).Location = New Point( _
        (posX * offset) - offset, _
        (posY * offset) - offset)

    Debug.Print("t=" + t.ToString + " x=" + posX.ToString + " y=" + posY.ToString)

    Panel1.Controls.Add(labels(t))
Next t

Wanted result of calculation "column" and "row" from which I can calculate control's position will be:
1-1,  2-1,  3-1
1-2,  2-2,  3-2
etc... to...
1-12, 2-12, 3-12

According to my code posX is calculated well (123123123123...) but posY is not and should be 111222333...
Please help to get a calculation of posY properly.

It looks to me that you probably want...
Int ((t - 1) / 3) + 1

for your Y-coordinate calculation; I don't know what the Mod 12 part is for but you I expect you could fit that in if required.
Edit - Int() is required, not CInt(), as Int rounds down, but CInt rounds to the nearest.

Your solution gives me 11, 222, 333... instead of 111,222,333.

@user973238, Ah. You're quite correct. I should have said Int() not CInt()

@user973238, isn't it? I have it quite a lot in my code. But that's probably another question.

@user973238 you could try CInt(Math.Floor()) if Int() isn't working.
