# checking Integer uniqueness

I am currently doing an assignment which i am creating a program which will check all Lotto draws from a comma delimeted text file.  On the GUI i have 6 Combo boxes which display numbers 1 - 45  somehow i need to come up with a way to check that when the user selects a number, it will then update the available comboboxes so the user cannot select the same number twice.  Does anyone have any ideas.

I as thinking of an array with a nested for loop which compares the values and then sets a boolen to false or true however i cannot do this  because it will check itself and therefore send false result.

Anyone have any ideas....

Thanks
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Business Systems Analyst , ex-Senior Application EngineerCommented:
The easiest way maybe is using a ListBox, after you randomly get a Number, just delete the item from the ListBox, and then the same Number wont occurs twice. Understand?
0
Business Systems Analyst , ex-Senior Application EngineerCommented:
Example:

Private Sub Command1_Click()
Dim pos As Integer, myDraw As String
Randomize
myDraw = ""
For i = 1 To 6
pos = Int(Rnd * List1.ListCount)
myDraw = myDraw & List1.List(pos) & " "
List1.RemoveItem pos
Next i
myDraw = Trim\$(myDraw)
MsgBox "My Draw is : " & myDraw
End Sub

List1.Clear
For i = 1 To 45
Next i
List1.Visible = False
End Sub

cheers
0
Commented:
Or if you still want to use combo boxes:

Dim i As Integer
'Populate comboxes with numbers
For i = 1 To 30
Next i
End Sub

Private Sub Combo1_Click()
'Deletes item clicked in combo1 from combo2
Combo2.RemoveItem (Combo1.ListIndex)
End Sub

Does this help?
0
High School Computer Science, Computer Applications, and Mathematics TeachersCommented:
I think removing the currently selected number from the other comboboxes/listboxes is a bad idea.

What happens when the user changes their mind?  Now you have to track what number was in each box before it was changed, and then put that old number back into all of their selection pools in the correct order!

The user needs to press a button to confirm their selections.  It is then that you should validate their selection choices with a function.

Private Function selectedNumbersAreValid() As Boolean
Dim i As Integer
Dim a As Integer

For i = Combo1.LBound To Combo1.UBound
For a = Combo1.LBound To Combo1.UBound
If a <> i Then
If Combo1(i).Text = Combo1(a).Text Then
selectedNumbersAreValid = False
Exit Function
End If
End If
Next a
Next i
selectedNumbersAreValid = True
End Function

I think the most intuitive way to do this is to use only two listboxes.  Load up the first listbox with your numbers.  Move the numbers back and forth between the boxes when they are double clicked like this:

Option Explicit

Dim i As Integer

For i = 1 To 45
Next i
End Sub

Private Sub List1_DblClick()
Dim selIndex As Integer
selIndex = List1.ListIndex
If selIndex <> -1 Then
If List2.ListCount < 6 Then
List1.RemoveItem selIndex
End If
End If
End Sub

Private Sub List2_DblClick()
Dim selIndex As Integer
Dim value As Integer
Dim i As Integer
Dim v As Integer

selIndex = List2.ListIndex
If selIndex <> -1 Then
value = CInt(List2.List(selIndex))
List2.RemoveItem selIndex
For i = 0 To List1.ListCount - 1
v = CInt(List1.List(i))
If value < v Then
Exit Sub
End If
Next i
End If
End Sub

Private Sub Command1_Click()
If List2.ListCount = 6 Then
MsgBox "Thank you"
Else
MsgBox "You must select six numbers"
End If
End Sub

Regards,

Idle_Mind
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
or.. just prevent numbers from being selected that are currently in use. The Validate() event fires after focus has left and data is changed so you can check there. If you change the event's Cancel parameter to true, it'll force the user to select a different value.

Form1:
-----------------

Private Sub Combo1_Validate(Index As Integer, Cancel As Boolean)
Dim iNum As Integer, iLoop1 As Integer
'If the ComboBox value is numeric.. (ie: not "Select a number")
If IsNumeric(Combo1(Index).List(Combo1(Index).ListIndex)) = True Then
iNum = Combo1(Index).List(Combo1(Index).ListIndex)
'check if the new number is the same as any other
For iLoop1 = Combo1.lBound To Combo1.UBound
'If the current combobox isn't the same as the one
'that's changing.. (avoid checking itself)
If iLoop1 <> Index Then
'If a number was select in this box already, check it..
If Combo1(iLoop1).ListIndex <> -1 Then
'If the number is the same as the one we're using..
If CInt(Combo1(iLoop1).List(Combo1(iLoop1).ListIndex)) = iNum Then
'Cancel it..
Cancel = True
Call MsgBox("Select a different number.")
Exit Sub
End If
End If
End If
Next iLoop1
End If
End Sub
Dim iLoop1 As Integer, iLoop2 As Integer
For iLoop1 = Combo1.lBound To Combo1.UBound
Combo1(iLoop1).Text = "Select a number"
For iLoop2 = 1 To 10
Next iLoop2
Next iLoop1
End Sub
0
Commented:
Try this, just add a blank item at the combo boxes.

Private Sub cmbNumber_Click(Index As Integer)
Dim i               As Integer
Dim blnFound        As Boolean
i = cmbNumber.LBound
Found = False
While Not (i > cmbNumber.UBound) _
And Not blnFound
'Dont compare combobox against itself or if it space
If Not i = Index Then
If Not cmbNumber(i) = " " Then
If cmbNumber(i).Text = cmbNumber(Index).Text Then
cmbNumber(Index).Text = " "
blnFound = True
End If
End If
End If
i = i + 1
Wend
End Sub
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.