sorting comboboxes - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

sorting comboboxes

robertd33robertd33 Posts: 15Member
can i get a hint?... how can i sort multiple comboboxes according to the contents of one combobox?

sorting one combobox, easy
cbo1.Sorted = True

how can i sort cbo2, cbo3, cbo4 according to what is in cbo1? as in... i want all the
index 1's to change with cbo1's index,
index 2's to change with cbo1's index, etc.

Comments

  • kainsworthkainsworth Posts: 200Member
    [b][red]This message was edited by kainsworth at 2004-4-29 0:31:46[/red][/b][hr]
    [b][red]This message was edited by kainsworth at 2004-4-29 0:25:54[/red][/b][hr]
    Is this the kind of thing you're looking for:
    [code]
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    With ComboBox1
    .Items.Add("Flour")
    .Items.Add("Water")
    .Items.Add("Sugar")
    End With
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    ComboBox1.Sorted = True
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    ComboBox2.Items.Clear()
    Dim i As String
    For Each i In ComboBox1.Items
    ComboBox2.Items.Add(i)
    Next
    End Sub
    [/code]

    It's the button2 click event that I think is the code you need (I've only included the setup code so you can copy and paste a complete sample).

    You can expand this basic idea so that you can update multiple comboboxes in one sweep. A procedure something like this:
    [code]
    Private Sub CopyToAllCombos(ByVal SourceCbo As ComboBox)
    Dim ctrl As Control
    ' Enumerate through every control on your form
    For Each ctrl In Me.Controls
    ' If it's a combobox then we're interested in it
    If TypeOf ctrl Is ComboBox Then
    ' If it's source combo then ignore it
    If Not ctrl.Name = SourceCbo.Name Then
    ' Declare some temp variables
    Dim i As String, cbo As ComboBox
    ' You can't use "ctrl.items." so have to create
    ' a temp combobox to pass in the Clear and Add methods
    cbo = ctrl
    ' remove any previous list
    cbo.Items.Clear()
    ' Copy all entries from source combo to this one
    For Each i In SourceCbo.Items
    cbo.Items.Add(i)
    Next
    End If
    End If
    ' Do same thing on next combobox on form
    Next
    End Sub[/code]
    would probably do the trick. You would need to decide what event to use to fire up the procedure and this would depend on when and how you are invoking the sort of the first combo - maybe a button press or a change of text or the addition of a new item, etc?

    The calling code would just be:
    [code]
    CopyToAllCombos(ComboBox1)
    [/code]

    Hope this helps

    Ged







    : can i get a hint?... how can i sort multiple comboboxes according to the contents of one combobox?
    :
    : sorting one combobox, easy
    : cbo1.Sorted = True
    :
    : how can i sort cbo2, cbo3, cbo4 according to what is in cbo1? as in... i want all the
    : index 1's to change with cbo1's index,
    : index 2's to change with cbo1's index, etc.
    :





Sign In or Register to comment.