by Z'Sisters ()
Category: Custom Controls/Forms/Menus
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Thu 28th March 2002
Date Added: Mon 8th February 2021
Rating: (1 Votes)
Puzzle using Control Array with easy and effective Code.
' Putting 15 Random Numbers in an Order
' place textbox control on the Form i.e. Text1
' set its index property as zero
' copy and paste it 15 times and site them as 4x4 matrix
' set text property of Control Array as 1,12,11,5,6,8,2,3,4,7,
' 9,14,10,13 and 15 for corresponding indexes 0 to 15 respectively.
' place two textboxes for testing & using top and left posotions
' of Control Array where the Cursor is presented
' place a Label Control to Display Caption as Puzzle
' set forecolor as red with Bolded fontsize 14
Private Sub Form_Activate()
Text2.Visible=False ' These Controls are not Interfaced By
' the User Action
Text3.Visible=False
Text1(15).SetFocus ' To put the Cursor at 16 TextBox
' Which is Empty
Text2.Text = Text1(15).Top
Text3 = Text1(15).Left
j = 15 ' for getting where the Cursor is Located
End Sub
' This Event is Triggered whenever Key is Released within TextControl Array
Private Sub Text1_KeyUp(Index As Integer, KeyCode As Integer, Shift As Integer)
' for finding top and left posotions where Cursor is Presented
For i = 0 To Text1.UBound
If i = Index Then
Text2 = Text1(i).Top
Text3 = Text1(i).Left
End If
Next i
Select Case KeyCode
Case vbKeyUp
For i = 0 To Text1.UBound ' Here 480 is the Differece b/w
' two textboxes top posotions(row by row)
If Text1(i).Top = Text2 - 480 And Text3 = Text1(i).Left Then
Text1(j) = Text1(i) ' i th TextBox Value is Copied
' into j th TextBox
Text1(i) = "" ' Making Empty
Text1(i).SetFocus ' for Focusing
j = i ' Persistence of Cursor Position
Exit For
End If
Next i
Case vbKeyDown
For i = 0 To Text1.UBound
If Text1(i).Top = Text2 + 480 And Text3 = Text1(i).Left Then
Text1(j) = Text1(i)
Text1(i) = ""
Text1(i).SetFocus
j = i
Exit For
End If
Next i
Case vbKeyLeft
For i = 0 To Text1.UBound ' Here 600 is Left Diff.
If Text1(i).Top = Text2 And Text1(i).Left = Text3 - 600 Then
Text1(j) = Text1(i)
Text1(i) = ""
Text1(i).SetFocus
j = i
Exit For
End If
Next i
Case vbKeyRight
For i = 0 To Text1.UBound
If Text1(i).Top = Text2 And Text1(i).Left = Text3 + 600 Then
Text1(j) = Text1(i)
Text1(i) = ""
Text1(i).SetFocus
j = i
Exit For
End If
Next i
End Select
End Sub