Option Explicit
Const cstNumberNW = 10
'Each movement of the scrollbar create a maximum if 10 new words
Const cstNumberTests = 100
'Do a maximum of 100 tests each search
Private Sub Form_Click()
lstList.Clear
End Sub
Private Sub Form_Load()
Randomize Timer
Me.Caption = "Word generator?"
End Sub
Private Sub lstList_Click()
lstList.RemoveItem lstList.ListIndex
End Sub
Private Sub Word_KeyPress(KeyAscii As Integer)
If KeyAscii = vbKeyReturn Then
'At least 2 signs
If Len(Word) > 1 Then
'Set scrollbar
hsbScroll.Max = Len(Word.Text)
hsbScroll.Min = 1
'\ gives a round number
hsbScroll.Value = hsbScroll.Max \ 2
lblLength.Caption = hsbScroll.Value
KeyAscii = 0
End If
End If
End Sub
Private Sub hsbScroll_Change()
If hsbScroll.Value > 1 And Len(Word) > 1 Then
'Only words of 2 or more signs
'Show the setup of the scrollbar
lblLength.Caption = hsbScroll.Value
'Declarations
Dim strSign As String, strFoundWord As String
Dim strTWord As String
Dim intNumberGenerated As Integer, intLFound As Integer
Dim intPosition As Integer, intListPointer As Integer
Dim intNumberLoops As Integer
'Number of words to be generated
intNumberGenerated = 1
Do Until intNumberGenerated = cstNumberNW + 1 Or _
intNumberLoops > cstNumberTests
intNumberLoops = intNumberLoops + 1
intLFound = 1
strTWord = Word
Do
'Choose a random sign from the word
strSign = Mid(strTWord, Int(Len(strTWord) _
* Rnd + 1), 1)
'Where in the word is the sign
intPosition = InStr(1, strTWord, strSign)
'Delete this sign from TWord becouse
'a sign may only be used once
Select Case intPosition
Case 1 'First sign
strTWord = Right(strTWord, _
Len(strTWord) - 1)
Case Len(strTWord) 'Last sign
strTWord = Left(strTWord, _
Len(strTWord) - 1)
Case Else 'Somewhere in the middle
strTWord = Left(strTWord, _
InStr(1, strTWord, strSign) _
- 1) & Right(strTWord, InStr(1, _
strSign, strTWord) + 1)
End Select
'Add sign
strFoundWord = strFoundWord + strSign
intLFound = intLFound + 1
Loop Until intLFound > hsbScroll.Value Or _
Len(strTWord) = 0
'Check if the found word is already in the list
intListPointer = 0
Do Until intListPointer = lstList.ListCount
intListPointer = intListPointer + 1
'If the word is in the list
If strFoundWord = lstList.List(intListPointer _
- 1) Then
strFoundWord = ""
intListPointer = lstList.ListCount
End If
Loop
'Add a new word to the list
If Len(strFoundWord) Then
lstList.AddItem strFoundWord
Me.Caption = "Word generator, generated: " & _
lstList.ListCount & " words so far"
DoEvents 'Give windows the chance
'to edit the form
strFoundWord = ""
End If
Me.Caption = "Word generator, so far: " & lstList.ListCount & _
" / Test: " & intNumberLoops
Loop
End If
End Sub