Solved

checking Integer uniqueness

Posted on 2004-04-15
9
156 Views
Last Modified: 2010-05-02
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
0
Comment
Question by:innovisiontechnology
9 Comments
 
LVL 49

Expert Comment

by:Ryan Chong
Comment Utility
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
 
LVL 49

Expert Comment

by:Ryan Chong
Comment Utility
Example:

Private Sub Command1_Click()
    Dim pos As Integer, myDraw As String
    Form_Load
    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

Private Sub Form_Load()
    List1.Clear
    For i = 1 To 45
        List1.AddItem i
    Next i
    List1.Visible = False
End Sub

cheers
0
 
LVL 5

Expert Comment

by:Pi7
Comment Utility
Or if you still want to use combo boxes:


Private Sub Form_Load()
Dim i As Integer
'Populate comboxes with numbers
For i = 1 To 30
Combo1.AddItem i
Combo2.AddItem i
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 25 total points
Comment Utility
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

Private Sub Form_Load()
    Dim i As Integer
           
    For i = 1 To 45
        List1.AddItem i
    Next i
End Sub

Private Sub List1_DblClick()
    Dim selIndex As Integer
    selIndex = List1.ListIndex
    If selIndex <> -1 Then
        If List2.ListCount < 6 Then
            List2.AddItem List1.List(selIndex)
            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
                List1.AddItem value, i
                Exit Sub
            End If
        Next i
        List1.AddItem value
    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
 
LVL 17

Assisted Solution

by:zzzzzooc
zzzzzooc earned 25 total points
Comment Utility
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:
-----------------

'Add 6 ComboBoxes.. Indexes 0->5
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
Private Sub Form_Load()
    Dim iLoop1 As Integer, iLoop2 As Integer
    'Just add test items
    For iLoop1 = Combo1.lBound To Combo1.UBound
        Combo1(iLoop1).Text = "Select a number"
        For iLoop2 = 1 To 10
            Combo1(iLoop1).AddItem iLoop2
        Next iLoop2
    Next iLoop1
End Sub
0
 
LVL 3

Expert Comment

by:redfordb
Comment Utility
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

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now