[RESOLVED] Radio ComboBox Group (Changing value on one combo affects others)-VBForums
Results 1 to 12 of 12

Thread: [RESOLVED] Radio ComboBox Group (Changing value on one combo affects others)

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2013
    Posts
    25

    Resolved [RESOLVED] Radio ComboBox Group (Changing value on one combo affects others)

    Hey all,

    I have a rather strange request, or well... I am requesting something that could possibly have an alternative solution.

    I have 3 ComboBoxes all with the same data, and each ComboBox has 3 elements. Suppose, for example, the 3 elements are "APPLES", "ORANGES", "BANANAS".

    So what I am seeking to do is to take the Click() event from the ComboBoxes (note that these are in a Control array) and use it so that when one ComboBox needs to change its value, all other ComboBox'es will not contain that same selected text.

    If the first combobox's text = APPLES, the second = ORANGES, and the third = BANANAS;
    When I manually change the third combobox's text to be APPLES, I want the first combobox's text to change to be whatever option was not selected (in this case it'd be BANANAS)
    Does this make sense?

    Anyways, I am having difficulty with this algorithm, which is mainly what I am seeking.
    Current I know how to detect which combobox needs to change, I just don't know how to get the correct value to change it to:
    Code:
    Private Sub ComboBoxes_Click(Index As Integer)
        Dim idx As Integer
        For idx = 0 To 2
            If ComboBoxes(idx).ListIndex = ComboBoxes(Index).ListIndex Then 'If there is a combobox that is currently using the newly selected index...
                'ComboBoxes(idx).ListIndex = newIndex  '<-- What is the value of newIndex?
                Exit For
            End If
        Next
    End
    PS: It's a good idea to know that whenever you programmatically change the ListIndex of a combo, the Click() event is triggered.

    I appreciate all the help you can give.

    Thanks and God Bless!
    -Nick


    View Solution
    Last edited by Millerni456; Mar 1st, 2013 at 01:16 PM.

  2. #2
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    6,357

    Re: Radio ComboBox Group (Changing value on one combo affects others)

    I know you can probably write this a little simpler (less code, that is, but maybe not more simpler looking):

    EDIT....read your post incorrectly...will be right back......

  3. #3
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    6,357

    Re: Radio ComboBox Group (Changing value on one combo affects others)

    Quick question...will there always be only three different fruits with three comboboxes? Or is there a possibility that there will be more fruits than boxes?

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jan 2013
    Posts
    25

    Re: Radio ComboBox Group (Changing value on one combo affects others)

    The contents in the combobox are NOT dynamic. So if there was initially 3 elements, then there will always be 3 elements.

    EDIT: The intention is to have the same number of boxes as there are options.

    The program I'm making has a list of tasks that need to be completed in a specific order.
    So all the tasks are the same in each combobox, I just need to select which item belongs in what position.

    I'm really hoping to do this with as small of space as possible, so I chose to use 3 labels and 3 combobox which correspond with the three order positions.
    Last edited by Millerni456; Mar 1st, 2013 at 10:40 AM.

  5. #5
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    6,357

    Re: Radio ComboBox Group (Changing value on one combo affects others)

    Will this work for you?

    EDIT: ARGHH..doesn't quite work...but you get the idea....play with this in the debugger and you'll find my mistakes.......

    Code:
    Option Explicit
    
    Dim theFruit As String, cbox0 As String, cbox1 As String, cbox2 As String
    
    Private Sub Form_Load()
    Dim x As Integer
    For x = 0 To 2
       comboboxes(x).AddItem ("Orange")
       comboboxes(x).AddItem ("Apple")
       comboboxes(x).AddItem ("Banana")
    Next x
    comboboxes(0).Text = "Orange"
    comboboxes(1).Text = "Apple"
    comboboxes(2).Text = "Banana"
    End Sub
    Private Sub comboboxes_DropDown(Index As Integer)
    theFruit = comboboxes(Index).Text
    End Sub
    
    Private Sub ComboBoxes_Click(Index As Integer)
        cbox0 = comboboxes(0).Text
        cbox1 = comboboxes(1).Text
        cbox2 = comboboxes(2).Text
    Select Case Index
    Case 0
    
        If cbox0 = cbox1 Then
            comboboxes(2).Text = theFruit
            comboboxes(1).Text = cbox2
        ElseIf cbox0 = cbox2 Then
            comboboxes(1).Text = theFruit
            comboboxes(2).Text = cbox1
        End If
    Case 1
        If cbox1 = cbox2 Then
            comboboxes(0).Text = theFruit
            comboboxes(2).Text = cbox0
        ElseIf cbox1 = cbox0 Then
            comboboxes(2).Text = theFruit
            comboboxes(0).Text = cbox2
    End If
    Case 2
        If cbox2 = cbox0 Then
            comboboxes(1).Text = theFruit
            comboboxes(0).Text = cbox1
        ElseIf cbox1 = cbox0 Then
            comboboxes(0).Text = theFruit
            comboboxes(1).Text = cbox2
    End If
    End Select
    
    End Sub
    Last edited by SamOscarBrown; Mar 1st, 2013 at 11:01 AM.

  6. #6
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    6,357

    Re: Radio ComboBox Group (Changing value on one combo affects others)

    I found it.....

    Code:
    Case 2
        If cbox2 = cbox0 Then
            comboboxes(1).Text = theFruit
            comboboxes(0).Text = cbox1
        ElseIf cbox2 = cbox1 Then
            comboboxes(0).Text = theFruit
            comboboxes(1).Text = cbox0

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Jan 2013
    Posts
    25

    Resolved Re: Radio ComboBox Group (Changing value on one combo affects others)

    That's really close SamOscarBrown. Thanks for that post.
    The issue with it is that it becomes a little unpredictable when scrolling (although I never mentioned anything about scrolling above).

    However, I was able to tackle this problem using a different algorithm:

    Code:
    Private combosInitialized As Boolean 'Stores whether or not the combobox's have been intialized.
    Private oldIdx As Integer 'Stores the combobox's index before the value is changed.
    
    Private Sub ComboBoxes_GotFocus(Index As Integer)
        oldIdx = ComboBoxes(Index).ListIndex 'When focused, we need to store the currently selected idx so we know what its value changed from.
    End Sub
    
    Private Sub ComboBoxes_Click(Index As Integer) 'This index refers to the clicked combobox.
        Dim idx As Integer
        If combosInitialized Then
            For idx = 0 To 2 'Iterate through all of the ComboBoxes
                'If there is a combobox that is currently using the newly selected index, and we are not looking at the box we just changed.
                If ComboBoxes(idx).ListIndex = ComboBoxes(Index).ListIndex And idx <> Index Then
                    ComboBoxes(idx).ListIndex = oldIdx 'This modifies the combox that contained the value that we just selected (swapping is here).
                    Exit For 'Exit the loop since there is no need to continue searching for the combobox to swap values with
                End If
            Next
            Call ComboBoxes(Index).SetFocus 'Re-focus on this currently selected combobox (updates its oldIdx)
        End If
    End Sub
    Now, when using this code, you need to make sure that the bounds match the number of comboboxes, and to make sure that all comboboxes have the same data inside, and for the same list indices.
    You also need to make sure that all of the comboboxes have the ListStyle of 2 (which is Dropdown List). When you initialize your comboboxes, you need to make each combobox have a different ListIndex. After setting all of the list indices you must set "combosInitialized = True". See code below:
    Code:
    Public Sub Form_Load()
        ComboBoxes(0).ListIndex = 0
        ComboBoxes(1).ListIndex = 1
        ComboBoxes(2).ListIndex = 2
        combosInitialized = True
    End Sub
    Thanks again for your help. It put me closer to the right direction!

    God Bless,
    -Nick
    Last edited by Millerni456; Mar 1st, 2013 at 11:41 AM.

  8. #8
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    6,357

    Re: Radio ComboBox Group (Changing value on one combo affects others)

    Close? Seems to work okay for me...and what do you mean by scrolling? Anyway....you are welcome....good luck with your avenue of approach.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Jan 2013
    Posts
    25

    Re: Radio ComboBox Group (Changing value on one combo affects others)

    When selecting a combo box, I tried scrolling through the items (using the mouse wheel), I would start getting duplicate values.

    This is most likely because when scrolling, you do not trigger the "DropDown" event that you used. But aside from scrolling... it works perfectly!

  10. #10
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,118

    Re: [RESOLVED] Radio ComboBox Group (Changing value on one combo affects others)

    I'm sure I'm over thinking this but this seems to work.
    Code:
    Private Sub ComboBoxes_Click(Index As Integer)
    Dim i As Integer
    Dim x As Integer
    Dim intNewSelection As Integer
    Dim intIndexToChange As Integer
        
        intIndexToChange = -1
        
        For i = 0 To 2
            If (i <> Index) And (ComboBoxes(i).ListIndex = ComboBoxes(Index).ListIndex) Then
                intIndexToChange = i         
                intNewSelection = 3
    
                For x = 0 To 2
                    If x <> intIndexToChange Then
                        intNewSelection = intNewSelection - ComboBoxes(x).ListIndex
                    End If
                Next x
        
                ComboBoxes(intIndexToChange).ListIndex = intNewSelection
            End If
        Next i
    
    End Sub
    
    Private Sub Form_Load()
    Dim i As Integer
    
        For i = 0 To 2
            ComboBoxes(i).AddItem "APPLES"
            ComboBoxes(i).AddItem "ORANGES"
            ComboBoxes(i).AddItem "BANANAS"
            
            ComboBoxes(i).ListIndex = i
        Next i
    End Sub
    NOTE: Seems to work with the mouse scroll.
    Last edited by MarkT; Mar 1st, 2013 at 12:17 PM. Reason: Added Note

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Jan 2013
    Posts
    25

    Re: [RESOLVED] Radio ComboBox Group (Changing value on one combo affects others)

    Hi MarkT,

    This does exactly what I was trying to do.
    To elaborate a little bit on your code, it's important to highlight this value: "intNewSelection = 3"

    The "3" here is indicating the sum of all possible indices. So if we wanted to add a fourth combo box, this value needs to be "6"; a fith combo box requires "10" etc...
    The sum of indices for 3 combo boxes: 0 + 1 + 2 = 3.

    I like the initialization, it saves the work of hardcoding into the Form Designer!

    Thanks much to all of you for your efforts!

  12. #12
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,118

    Re: [RESOLVED] Radio ComboBox Group (Changing value on one combo affects others)

    If you want to have it so you don't need to provide a number for intNewSelection then you can do this and it won't care how many combos you have.
    Code:
    Private Sub ComboBoxes_Click(Index As Integer)
    Dim i As Integer
    Dim x As Integer
    Dim intNewSelection As Integer
    Dim intIndexToChange As Integer
    Dim n As Integer
    
        
        intIndexToChange = -1
        n = ComboBoxes.Count - 1
        
        For i = 0 To n
            If (i <> Index) And (ComboBoxes(i).ListIndex = ComboBoxes(Index).ListIndex) Then
                intIndexToChange = i
                
                intNewSelection = n * (n + 1) / 2
                
                For x = 0 To n
                    If x <> intIndexToChange Then
                        intNewSelection = intNewSelection - ComboBoxes(x).ListIndex
                    End If
                Next x
        
                ComboBoxes(intIndexToChange).ListIndex = intNewSelection
            End If
        Next i
    End Sub

Tags for this Thread

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.