Solved

checking Integer uniqueness

Posted on 2004-04-15
9
160 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 51

Expert Comment

by:Ryan Chong
ID: 10831693
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 51

Expert Comment

by:Ryan Chong
ID: 10831717
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
ID: 10831855
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 25 total points
ID: 10832968
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
ID: 10839203
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
ID: 10840589
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

808 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