Solved

Populate Combo box from treeview

Posted on 2011-09-25
9
630 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
ID: 36596431
Isn't that kind of populating the combobox just like the treeview?

0
 
LVL 61

Expert Comment

by:mbizup
ID: 36596459
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
ID: 36596509
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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 21
ID: 36596612
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
 
LVL 33

Expert Comment

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

0
 

Author Comment

by:stevensont
ID: 36596673
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
ID: 36596687
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
ID: 36596996
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
ID: 36890360
close.
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

809 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