|
|
Title | Manage a 2-dimensional control array |
Keywords | control array, two-dimensional, 2D |
Categories | Software Engineering, Controls |
|
|
Map the control index in a one-dimensional control array to row and column. If the number of rows and columns are NUM_ROWS and NUM_COLS, then calculate row and column values using the formulas:
r = Index \ NUM_COLS
c = Index Mod NUM_COLS
Calculate index from row and column using:
Index = r * NUM_COLS + c
|
|
Private Const NUM_ROWS = 10
Private Const NUM_COLS = 4
Private Const MAX_ROW = NUM_ROWS - 1
Private Const MAX_COL = NUM_COLS - 1
Private strings() As String
Private Sub Form_Load()
Const GAP = 60
Dim X As Single
Dim Y As Single
Dim wid As Single
Dim hgt As Single
Dim r As Integer
Dim c As Integer
Dim i As Integer
' Make a string array to match the TextBoxes.
ReDim strings(0 To MAX_ROW, 0 To MAX_COL)
' Create and position the controls.
X = Text1(0).Left
Y = Text1(i).Top
wid = Text1(i).Width
hgt = Text1(i).Height
' Create the controls.
For i = 1 To NUM_ROWS * NUM_COLS - 1
Load Text1(i)
If i Mod NUM_COLS = 0 Then
' Start a new row.
X = Text1(0).Left
Y = Y + hgt + GAP
Else
X = X + wid + GAP
End If
Text1(i).Move X, Y, wid, hgt
Text1(i).Visible = True
Text1(i).Text = i
Next i
Text1(0).Text = "0"
End Sub
' Update the corresponding string.
Private Sub Text1_Change(Index As Integer)
Dim r As Integer
Dim c As Integer
r = Index \ NUM_COLS
c = Index Mod NUM_COLS
strings(r, c) = Text1(Index).Text
End Sub
' Change the color of the TextBox just to verify
' that our row and column calculations are right.
Private Sub Text1_Click(Index As Integer)
Dim r As Integer
Dim c As Integer
r = Index \ NUM_COLS
c = Index Mod NUM_COLS
Index = r * NUM_COLS + c
If Text1(Index).ForeColor = vbBlack Then
Text1(Index).ForeColor = vbRed
Else
Text1(Index).ForeColor = vbBlack
End If
End Sub
|
|
|
|
|
|