You have to do it manually
This code will create and position the command buttons for you. What you want though is to have the Index Property of the command button to support multi-dimensions which I do not think you will be able to do.
Code:
' form has one command button (command1) which has the
' visible property = false and index=0
Option Explicit
Dim mMultiArray(1 To 3, 1 To 3) As CommandButton
Private Sub Form_Load()
ScaleMode = 3
Dim x As Integer
Dim y As Integer
Dim myButton As CommandButton
For y = 1 To 3
For x = 1 To 3
Load Command1((y - 1) * 3 + x)
Set mMultiArray(x, y) = Command1((y - 1) * 3 + x)
mMultiArray(x, y).Visible = True
mMultiArray(x, y).Left = x * 50
mMultiArray(x, y).Top = y * 50
Next x
Next y
End Sub
' now you can loop through your buttons at will.
hehe, it should be so easy :D
You have to dim withevents and it will not let you dim withevents a multidimitional array. Unless I am overlooking something. If anyone knows how you could have a multidimitional array of controls. please tell us.
Thanks,
KillemAll
hehehe, I humbly beg your forgiveness...
Points all well taken there Paul. Had a brain siezure or something. I didn't see that you have basically created a multi dimintional array of controls with your code. I will have to file that away for future use. The only advantage I was looking for was to loop through the columns and rows of buttons. I tried to do the same thing you did but I did not have the Load and Set statements. So I figured you could not use the events of them. I figured you had to dim them "WithEvents". Thanks again for straightening me out.
here is my revised attempt based on your code.
Code:
Option Explicit
Dim mMultiArray(1 To 3, 1 To 3) As CommandButton
Private Sub Form_Load()
ScaleMode = 3
Dim x As Integer
Dim y As Integer
Dim myButton As CommandButton
For y = 1 To 3
For x = 1 To 3
Load Command1((y - 1) * 3 + x)
Set mMultiArray(x, y) = Command1((y - 1) * 3 + x)
mMultiArray(x, y).Visible = True
mMultiArray(x, y).Left = x * 50
mMultiArray(x, y).Top = y * 50
mMultiArray(x, y).Caption = "-"
Next x
Next y
End Sub
Private Sub Command1_Click(Index As Integer)
Dim x As Integer
Dim y As Integer
x = (Index - 1) Mod 3 + 1
y = Int((Index - 1) / 3) + 1
Command1(Index).Caption = "X"
Dim iCntHor As Integer, iCntVer As Integer
Dim Test As String
For iCntHor = 1 To 3
For iCntVer = 1 To 3
Test = Test & mMultiArray(iCntHor, iCntVer).Caption
Next
If Test = "XXX" Then
MsgBox "You Win"
Unload Me
Exit Sub
End If
Test = ""
Next
For iCntVer = 1 To 3
For iCntHor = 1 To 3
Test = Test & mMultiArray(iCntHor, iCntVer).Caption
Next
If Test = "XXX" Then
MsgBox "You Win"
Unload Me
Exit Sub
End If
Test = ""
Next
If mMultiArray(1, 1).Caption & mMultiArray(2, 2).Caption & mMultiArray(3, 3).Caption = "XXX" Then
MsgBox "You Win"
Unload Me
Exit Sub
ElseIf mMultiArray(1, 3).Caption & mMultiArray(2, 2).Caption & mMultiArray(3, 1).Caption = "XXX" Then
MsgBox "You Win"
Unload Me
Exit Sub
End If
End Sub