VERSION 5.00
Begin VB.Form Form1 
   BorderStyle     =   1  'Fixed Single
   Caption         =   "Form1"
   ClientHeight    =   5955
   ClientLeft      =   45
   ClientTop       =   330
   ClientWidth     =   6045
   LinkTopic       =   "Form1"
   MaxButton       =   0   'False
   ScaleHeight     =   5955
   ScaleWidth      =   6045
   StartUpPosition =   3  'Windows Default
   Begin VB.ListBox lstResults 
      Height          =   5520
      Left            =   3720
      TabIndex        =   11
      Top             =   240
      Width           =   2175
   End
   Begin VB.CommandButton cmdGenerate 
      Caption         =   "Generate combinations"
      Height          =   615
      Left            =   360
      TabIndex        =   8
      Top             =   4800
      Width           =   3015
   End
   Begin VB.Frame Frame1 
      Height          =   3375
      Left            =   120
      TabIndex        =   0
      Top             =   120
      Width           =   3495
      Begin VB.TextBox txtMaxLimit 
         Height          =   285
         Left            =   840
         TabIndex        =   4
         Text            =   "200"
         Top             =   2400
         Width           =   1815
      End
      Begin VB.TextBox txtNumbers 
         Height          =   285
         Left            =   1440
         TabIndex        =   3
         Text            =   "4"
         Top             =   720
         Width           =   495
      End
      Begin VB.TextBox txtUptoVal 
         Height          =   285
         Left            =   1440
         TabIndex        =   2
         Text            =   "3"
         Top             =   1560
         Width           =   495
      End
      Begin VB.CommandButton CmdCalc 
         Caption         =   "Calculate maximum combinations limit"
         Height          =   375
         Left            =   360
         TabIndex        =   1
         Top             =   2760
         Width           =   2775
      End
      Begin VB.Label Label3 
         Alignment       =   2  'Center
         BackStyle       =   0  'Transparent
         Caption         =   "Maximum combinations limit"
         Height          =   255
         Left            =   720
         TabIndex        =   7
         Top             =   2160
         Width           =   2055
      End
      Begin VB.Label Label1 
         Alignment       =   2  'Center
         BackStyle       =   0  'Transparent
         Caption         =   "Numbers in each combination"
         Height          =   255
         Index           =   1
         Left            =   480
         TabIndex        =   6
         Top             =   480
         Width           =   2535
      End
      Begin VB.Label Label1 
         Alignment       =   2  'Center
         BackStyle       =   0  'Transparent
         Caption         =   "Random number upper limit"
         Height          =   255
         Index           =   2
         Left            =   720
         TabIndex        =   5
         Top             =   1320
         Width           =   2055
      End
   End
   Begin VB.Label Label1 
      BackStyle       =   0  'Transparent
      Caption         =   "Combinations generated:"
      Height          =   255
      Index           =   3
      Left            =   360
      TabIndex        =   10
      Top             =   4200
      Width           =   1815
   End
   Begin VB.Label Label2 
      Alignment       =   2  'Center
      BackColor       =   &H00000000&
      BorderStyle     =   1  'Fixed Single
      BeginProperty Font 
         Name            =   "MS Sans Serif"
         Size            =   12
         Charset         =   0
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      ForeColor       =   &H00FFFFFF&
      Height          =   375
      Left            =   2160
      TabIndex        =   9
      Top             =   4080
      Width           =   1215
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Private Declare Function LockWindowUpdate Lib "user32" (ByVal hwndLock As Long) As Long

Private Sub CmdCalc_Click()
If Val(txtUptoVal) = 0 Or Val(txtNumbers) = 0 Then
    txtMaxLimit.Text = "0"
Else
    txtMaxLimit.Text = Format$(Val(txtUptoVal) ^ Val(txtNumbers), "###,###,###,###,###,##0")

End If

End Sub

'A.A get a list random combinations of chosen length in numbers (will always return the same amounts of numbers specified!)
Private Sub cmdGenerate_Click()
Dim a&, b&, c&
a& = Val(txtMaxLimit)
b& = Val(txtNumbers)
c& = Val(txtUptoVal)
    Screen.MousePointer = vbHourglass
    lstResults.Clear
    txtMaxLimit.BackColor = &HC0C0C0
    txtUptoVal.BackColor = &HC0C0C0
    txtNumbers.BackColor = &HC0C0C0
    cmdGenerate.Enabled = False
    LockWindowUpdate lstResults.hWnd
    Call GetRndCombos(lstResults, b&, c&, a&, Label2)
    Screen.MousePointer = vbDefault
    txtMaxLimit.BackColor = vbWhite
    txtUptoVal.BackColor = vbWhite
    txtNumbers.BackColor = vbWhite
    cmdGenerate.Enabled = True
    LockWindowUpdate 0&
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Set Form1 = Nothing
End Sub

Private Function GetRndCombos(ByVal lst As ListBox, ByVal Numbers As Integer, ByVal UpToNum As Integer, Optional ByVal MaxCombos As Long, Optional ByVal lbl As Label) As String
'You should make this function as public and place it inside a module.

'*************************************************************************
'* RANDOM COMBINATION GENERATOR (GetRndCombos) By André Aylestock June 2003
'*************************************************************************
'* PURPOSE: To populate a listbox with random combinations of specified amount and of specified numbers in length.
'*************************************************************************
'* Numbers = amount of numbers you wish generated per each combination.
'* UpToNum = upper number limit that will be used by the Rnd functions.
'* MaxCombos = maximum amount of combinations that you wish to be generated.
'*************************************************************************
'* USAGE: Use example below to generate combinations of 3 numbers each ranging from 1 to 7
'* Call GetRndCombos(List1, 3, 7, 100) to generate only 100 combinations OR...
'* Call GetRndCombos(List1, 3, 7 ) to generate the maximum possible combinations (343)
'*************************************************************************
'* NOTE: When using large Number values, make sure to provide a way for your user to cancel the operation.
'* and also provide some form of visual feedback to the user when calling the function.
'*************************************************************************

Dim Combination As String, Delimiter As String
Dim i As Long, j As Long, Count As Long

If Numbers% = 0 Or UpToNum% = 0 Then Exit Function

StartOver:
      
If Count& >= (UpToNum% ^ Numbers%) Then Exit Function
    Combination$ = " "
    Randomize Timer
    
    For i& = 1 To Numbers%
    DoEvents
    Delimiter$ = IIf(i& = Numbers%, vbCrLf, ".")
    Combination$ = Combination$ & (Fix(Rnd(Timer) * UpToNum%)) + 1 & Delimiter$
    Next i&
 
   j = InStr(1, GetRndCombos$, Combination$, vbBinaryCompare)
  
If j Then
    'the combination generated returned a match so generate another one
    DoEvents
    GoTo StartOver
Else
   'the combination generated did not return a match so add it to the list
    lst.AddItem Mid(Combination$, 1, Len(Combination$) - 2)
    GetRndCombos$ = GetRndCombos$ & Combination$
    Count& = Count& + 1
    lbl.Caption = Count&
    If MaxCombos& = 0 Then GoTo StartOver
    If Count& < MaxCombos& Then GoTo StartOver
      

End If

End Function


