Results 1 to 7 of 7

Thread: Combobox array question?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    Angry

    1st thread removed.

    My situation:

    10 comboboxes(arrayed) on a form. On form load, loads all 50 states into the combobox and waits for user selection.

    My problem: Once the user picks a state in 1 of the 10, I would like the other 9 to exclude that state in it's selection. I fugured that would be easy enuff, but what if the user picked "Texas" in cboState(5), then I have to make cboState(0-4) then cboState(6-9) exclude "Texas". Can this be done?
    Remaining quiet down here !!!

    BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....

  2. #2

    Thread Starter
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    Talking Removing items from combobox

    Come on guys. I'm sure some can help me here...
    Remaining quiet down here !!!

    BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....

  3. #3
    Guest
    Try this:

    Code:
    Private Sub cboState_Change(Index As Integer)
    For i = 0 To 10
    cboState(i).Text = cboState(Index).Text
    Next i
    End Sub
    
    Private Sub cboState_Click(Index As Integer)
    For i = 0 To 10
    cboState(i).Text = cboState(Index).Text
    Next i
    End Sub

  4. #4
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177
    There are a few ways you could do this, here's one:
    Code:
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Private Const CB_FINDSTRINGEXACT = &H158
    
    Private Sub cboState_Click(Index As Integer)
        Dim oCombo As ComboBox
        Dim lIndex As Long
        Dim sFind As String
        
        'Value to match in other Combo's
        'Can;t use index number as it could be in
        'a different position for different combo's
        sFind = cboState(Index)
        
        'Enumerate each Combo in the Control Array
        For Each oCombo In cboState
            
            'Process every combo except the one the user changed
            If oCombo.Index <> Index Then
            
                'If the Combo had a previous value, add it back
                'to the lists of the other combo's
                If Val(cboState(Index).Tag) Then
                    oCombo.AddItem cboState(Index).List(Val(cboState(Index).Tag))
                End If
                
                'Find where the selected value appears in this combo
                lIndex = SendMessage(oCombo.hwnd, CB_FINDSTRINGEXACT, 0&, ByVal sFind)
                
                'If found, remove it
                If lIndex >= 0 Then oCombo.RemoveItem lIndex
            End If
        Next
        
        'Remember this value next time, so we can put it back
        cboState(Index).Tag = cboState(Index).ListIndex
    End Sub

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441
    Aaron Young:

    So, in theory, user selects Texas in one box and Texas is exluded in the other boxes, right? What if, in the same box, the user changes the selection to Arkansas? Would your code then add Texas back to the boxes it was once excluded from?
    Remaining quiet down here !!!

    BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....

  6. #6
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177
    Yes, it would, I'm using the Tag property to Track the Combo's previous value so I can replace it if necessary as you can see in this segment of the code:
    Code:
                'If the Combo had a previous value, add it back
                'to the lists of the other combo's
                If Val(cboState(Index).Tag) Then
                    oCombo.AddItem cboState(Index).List(Val(cboState(Index).Tag))
                End If

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441
    Aaron Young:

    Thank your for your response.
    Remaining quiet down here !!!

    BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width