I updated the code to store x and y in the button.Tag property instead of parsing button name. Also, no need for an array to store button names, since we can generate the button name for a given x and y value ob the form form1.buttonX_Y
, as sVB references control names to their forms. This is the new code:
For X = 1 To 8
For Y = 1 To 7
Btn = Me.AddButton(
"Button" + X + "_" + Y,
10 + 80 * X,
10 + 30 * Y,
80,
30
)
Btn.Text = X + "," + Y
Btn.Tag = {X, Y}
Btn.OnClick = Btn_Click
Btn.Visible = False
EndFor
Button1 = GetButton(4, 3)
Button1.Visible = True
EndFor
Sub Btn_Click()
For x = 1 To 8
For y = 1 To 7
Button1 = GetButton(x, y)
Button1.Visible = False
Next
Next
Button1 = Event.SenderControl
xy = Button1.Tag
ax = xy[1]
ay = xy[2]
ShowButtons(
{
{ax, ay},
{ax + 1, ay},
{ax - 1, ay},
{ax, ay + 1},
{ax, ay - 1}
}
)
EndSub
Sub ShowButtons(xyArr)
ForEach xy In xyArr
Button1 = GetButton(xy[1], xy[2])
Button1.Visible = True
Next
EndSub
Function GetButton(x, y)
Return "form1.button" + x + "_" + y
EndFunction