Link to home
Start Free TrialLog in
Avatar of stevensont
stevensontFlag for United States of America

asked on

Populate Combo box from treeview

I have a treeview and it works just fine. Now, what I'm trying to do is use the information in the treeview to populate a combo box. For example, if a user wants to add a new category, I want them to enter the information in a pop-up form and if the new category is a sub-category of an existing category, then I want them to be able to select the parent category from the combo box. This logic will also be used to change a category or sub-category. The thing about it is, I'm not sure where to begin because the combo box needs to be populated like this:
Category1
   sub1
   sub2
        sub2-sub1
        sub2-sub2
Category 2
...and so on

The categories should be lined up under each other (indented as appropriate) after a change the treeview need to be refreshed.
Avatar of Norie
Norie

Isn't that kind of populating the combobox just like the treeview?

While treeview controls are designed to show that kind of dependent relationship in a single control, combo boxes really are not - and formatting within a drop-down list can be cumbersome.

When using combo boxes, dependent data is generally shown with multiple "cascading" combo boxes.  For example, the user make a selection of "Category 1" in the first combo box, and that selection limits the options in combo2 to only those that pertain to Category 1.

Here's a tutorial:
http://www.blueclaw-db.com/comboboxlist/access_dependent_dropdown_list.htm
This code will populate a listbox with the child nodes of the node you click in a treeview.

Also if you click an item (which is sort of node) in the listbox then the listbox will be populated with the children of that item.

Note, it's pretty rough code but works for me with a simple treeview.
Private Sub ListBox1_Click()
Dim ndList As Node
Dim ndChild As Node

    If ListBox1.ListIndex <> -1 Then
        Set ndList = TreeView1.Nodes(Val(ListBox1.List(ListBox1.ListIndex, 1)))

        Set ndChild = ndList.Child
        If Not ndChild Is Nothing Then

            ListBox1.Clear
            Do

                ListBox1.AddItem ndChild.Text
                ListBox1.List(ListBox1.ListCount - 1, 1) = ndChild.Index
                Set ndChild = ndChild.Next

            Loop Until ndChild Is Nothing

        End If
    End If

End Sub

Private Sub TreeView1_NodeClick(ByVal Node As MSComctlLib.Node)
Dim ndChild As Node

    Set ndChild = Node.Child
    If Not ndChild Is Nothing Then

        ListBox1.Clear
        Do

            ListBox1.AddItem ndChild.Text
            ListBox1.List(ListBox1.ListCount - 1, 1) = ndChild.Index
            Set ndChild = ndChild.Next

        Loop Until ndChild Is Nothing

    End If
End Sub

Open in new window

I see more and  more web apps (shopping carts, CMS, blogs, etc)  using a combo box to do this. I also do it in Access.

 User generated image
Looks quite good but wouldn't it be tricky getting the indentation etc right.

Avatar of stevensont

ASKER

Hey TheHiTechCoach, that is exactly what I'm trying to do in an Access combo box. I agree with Imnorie, how to get the indentations etc right. You have an example of code that does that from a treeview?
Oops, just realised the code I posted wasn't for Access.

It should still work though.
ASKER CERTIFIED SOLUTION
Avatar of stevensont
stevensont
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
close.