PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
[VB 2013] Multiple comboboxes, same data, exclusive selections-VBForums
Results 1 to 3 of 3

Thread: [VB 2013] Multiple comboboxes, same data, exclusive selections

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2014
    Posts
    10

    [VB 2013] Multiple comboboxes, same data, exclusive selections

    So this seems like it should be fairly simple, but every solution I think up seems to just spiral out of control in complexity and become an unworkable mess.

    What I want is to have an arbitrary number of comboboxes drawing from the same set of choices. But the twist is, when one item is selected on a combobox, it is removed from the dropdown on all the rest. Conversely, when an item is deselected from one, it is restored to the rest.

    Any ideas?

  2. #2
    Addicted Member
    Join Date
    Mar 2007
    Posts
    183

    Re: [VB 2013] Multiple comboboxes, same data, exclusive selections

    Quote Originally Posted by jdlessl View Post
    So this seems like it should be fairly simple, but every solution I think up seems to just spiral out of control in complexity and become an unworkable mess.

    What I want is to have an arbitrary number of comboboxes drawing from the same set of choices. But the twist is, when one item is selected on a combobox, it is removed from the dropdown on all the rest. Conversely, when an item is deselected from one, it is restored to the rest.

    Any ideas?
    Seems like the best way to do that is to use a data source.
    Do not read this sentence.
    You read that last one, didn't you?
    Darn. You now read the previous two.

    Check out my pins on Pinterest!

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,719

    Re: [VB 2013] Multiple comboboxes, same data, exclusive selections

    This code will work with any number of ComboBoxes:
    vb.net Code:
    1. Private comboBoxes As ComboBox()
    2. Private items As String() = {"First", "Second", "Third", "Fourth", "Fifth"}
    3.  
    4. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    5.     'Put all the ComboBox controls on the form into an array.
    6.     Me.comboBoxes = Me.Controls.OfType(Of ComboBox)().ToArray()
    7.  
    8.     Array.ForEach(Me.comboBoxes,
    9.                   Sub(cb)
    10.                       'Populate all ComboBoxes with the entire list to start with.
    11.                       cb.Items.AddRange(Me.items)
    12.  
    13.                       'Handle the SelectedIndexChanged events of all ComboBoxes with the same method.
    14.                       AddHandler cb.SelectionChangeCommitted, AddressOf comboBoxes_SelectionChangeCommitted
    15.                   End Sub)
    16. End Sub
    17.  
    18. 'The SelectionChangeCommitted event is handled because it is only raised in response to user selections and not changes in code.
    19. Private Sub comboBoxes_SelectionChangeCommitted(sender As Object, e As EventArgs)
    20.     'Update the drop-down list of each ComboBox to include only items that are not selected in another ComboBox.
    21.     Array.ForEach(Me.comboBoxes, AddressOf UpdateDropDownList)
    22. End Sub
    23.  
    24. Private Sub UpdateDropDownList(current As ComboBox)
    25.     'Remember which item is selected so it can be reinstated after refreshing the drop-down list.
    26.     Dim selectedItem = current.SelectedItem
    27.  
    28.     'Get the items that are selected in another ComboBox.
    29.     Dim otherComboBoxes = Me.comboBoxes.Except({current})
    30.     Dim otherSelectedItems = otherComboBoxes.Select(Function(cb) cb.SelectedItem).Cast(Of String)()
    31.  
    32.     'Show all items that are not selected in another ComboBox.
    33.     current.Items.Clear()
    34.     current.Items.AddRange(Me.items.Except(otherSelectedItems).ToArray())
    35.  
    36.     'Reinstate the selected item.
    37.     current.SelectedItem = selectedItem
    38. End Sub
    The code assumes that all the ComboBoxes have been added directly to a form and not some other container, e.g. a GroupBox, and that the set comprises all the ComboBoxes on the form. Only small adjustments would be required if your scenario does not satisfy those requirements.

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