Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Populate Combo box from treeview

Posted on 2011-09-25
9
Medium Priority
?
700 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 35

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 35

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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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 35

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 35

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

618 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