' form code
Option Explicit
Private Const TOTALSHAPES = 200
Private Const SHAPESPERROW = 20
Private Const SHAPESIZE = 32
Dim TotalRows As Long
Private Sub Form_Load()
Dim lngA As Long, lngX As Long, lngY As Long
' change scalemode to pixels
Me.ScaleMode = vbPixels
Picture1.ScaleMode = vbPixels
Picture1.BorderStyle = vbBSNone
' init shapes
Shape1(0).Move 0, 0, SHAPESIZE, SHAPESIZE
Shape1(0).BackStyle = 1
Shape1(0).BorderStyle = 3
For lngA = 1 To (TOTALSHAPES - 1)
' load a new shape object, a copy of Shape1(0)
Load Shape1(lngA)
' do some simple math that determines X and Y location, multiply by shapesize
Shape1(lngA).Move SHAPESIZE * (lngA Mod SHAPESPERROW), SHAPESIZE * (lngA \ SHAPESPERROW), SHAPESIZE, SHAPESIZE
' just set some random color...
Shape1(lngA).BackColor = CLng(Rnd * vbWhite)
' make it visible so we can see it (by default new objects are hidden)
Shape1(lngA).Visible = True
Next lngA
' total rows
TotalRows = lngA \ SHAPESPERROW
' resize the picture area to match the size required to show the blocks
Picture1.Move 0, 0, SHAPESIZE * SHAPESPERROW, TotalRows * SHAPESIZE
End Sub
Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim lngX As Long, lngY As Long, lngIndex As Long
lngX = X \ SHAPESIZE
lngY = Y \ SHAPESIZE
' make sure we have valid X and Y
If lngX < 0 Then Exit Sub
If lngY < 0 Then Exit Sub
If lngX >= SHAPESPERROW Then Exit Sub
If lngY >= TotalRows Then Exit Sub
' calculate index
lngIndex = lngY * SHAPESPERROW + lngX
' show which index we are moving at
Me.Caption = "The current index is " & lngIndex
End Sub