Solved

checking Integer uniqueness

Posted on 2004-04-15
9
162 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 52

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 52

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 86

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

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month7 days, 2 hours left to enroll

622 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