Solved

Populate Combo box from treeview

Posted on 2011-09-25
9
596 Views
Last Modified: 2012-05-12
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.
0
Comment
Question by:stevensont
9 Comments
 
LVL 33

Expert Comment

by:Norie
Comment Utility
Isn't that kind of populating the combobox just like the treeview?

0
 
LVL 61

Expert Comment

by:mbizup
Comment Utility
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
0
 
LVL 33

Expert Comment

by:Norie
Comment Utility
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

0
 
LVL 21
Comment Utility
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.

 combo box example
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 33

Expert Comment

by:Norie
Comment Utility
Looks quite good but wouldn't it be tricky getting the indentation etc right.

0
 

Author Comment

by:stevensont
Comment Utility
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?
0
 
LVL 33

Expert Comment

by:Norie
Comment Utility
Oops, just realised the code I posted wasn't for Access.

It should still work though.
0
 

Accepted Solution

by:
stevensont earned 0 total points
Comment Utility
Well experts, here I found my answer. The author has great examples and explanations that I was quickly able to incorporate into my DB and it works just like described.
0
 

Author Closing Comment

by:stevensont
Comment Utility
close.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now