mctree Node Expander

davidmreid
NewLounger
Posts: 12
Joined: 19 Feb 2014, 01:45

mctree Node Expander

Post by davidmreid »

Could you please help me to understand the VBA Code to set the Expander icon to plus / minus for a specific node on the (mctree) treeview control? Thank you.

P.S. BTW: Kudos to Peter for the most excellent work on his Treeview.

User avatar
HansV
Administrator
Posts: 78415
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: mctree Node Expander

Post by HansV »

Welcome to Eileen's Lounge!

I assume that you mean the All-VBA TreeView control created by Jan Karel Pieterse with the help of Peter Thornton, available from An MSForms (all VBA) treeview.

What exactly do you want to accomplish?
Best wishes,
Hans

User avatar
Jan Karel Pieterse
Microsoft MVP
Posts: 656
Joined: 24 Jan 2010, 17:51
Status: Microsoft MVP
Location: Weert, The Netherlands

Re: mctree Node Expander

Post by Jan Karel Pieterse »

The state of the icon (whether present, whether plus or minus) is automatically chosen depending on whether or not the node in question has child nodes and whether or not it is expanded.
Regards,

Jan Karel Pieterse
Excel MVP jkp-ads.com

davidmreid
NewLounger
Posts: 12
Joined: 19 Feb 2014, 01:45

Re: mctree Node Expander

Post by davidmreid »

I set up the treeview to open to a default view as follows:

Cable One (Root)
View Data
-Select Report
-Choose Month
Export Data
-Choose Month
Import Data
-Select Report
-Choose Month

When I click a level one node (View Data, Import Data and Export Data) I am unable to capture the click event because those nodes are already opened. I want the child nodes to open/close when the level one node is clicked. A click of a level one node should open/close the the child nodes (Select Report / Choose Month) at the same time. Therefore I was trying to set the Expander Icon to Win7Minus as the form loads. Otherwise, the value for the "Expanded" property is always set to True. I figured that changing the "Expander" icon to minus when the tree is loading would make for less coding. I appreciate your help and suggestions. Thank you.

davidmreid
NewLounger
Posts: 12
Joined: 19 Feb 2014, 01:45

Re: mctree Node Expander

Post by davidmreid »

Following is how I solved the problem:

With Me.mcTree
cChild.Expanded = .Tag
.Tag = False
End With

But I thought perhaps there was similar code for changing the expander
like this code or similar that I found for the treeview common control:

TreeView1.SelectedItem.Child.selected = True
imageIndex = TreeView1.SelectedItem.Child.Key
TreeView1.SelectedItem.Child.Image = imageIndex

or kind of something simple like this with the outstanding TreeView control created by Jan Karel Pieterse with the help of Peter:
cNode.Image=iif(cNode.Level=1,"Win7Plus1","Win7Minus")

-David

User avatar
HansV
Administrator
Posts: 78415
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: mctree Node Expander

Post by HansV »

I'm not sure I understand what you're doing. To display the root only:

Code: Select all

        mcTree.ExpandToLevel 0
        mcTree.Refresh
To display only the root and the nodes immediately below it:

Code: Select all

        mcTree.ExpandToLevel 1
        mcTree.Refresh
etc.
This code should be called in the code module of the userform, after the treeview has been populated.
Best wishes,
Hans

davidmreid
NewLounger
Posts: 12
Joined: 19 Feb 2014, 01:45

Re: mctree Node Expander

Post by davidmreid »

Cool. Thanks Harris! I'll give it a try. -David

davidmreid
NewLounger
Posts: 12
Joined: 19 Feb 2014, 01:45

Re: mctree Node Expander

Post by davidmreid »

Hi Harris, Could you please show me how to expand / collapse the third parent node along with its two child nodes using ScrollToView and/or ExpandtoLevel? Thank you. -David

User avatar
HansV
Administrator
Posts: 78415
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: mctree Node Expander

Post by HansV »

Jan Karel may have to look into this. When I tried to call ExpandNode, Excel crashed. I think the code should be

Code: Select all

Public Sub ExpandNode(cNode As clsNode)
    Dim cTmp As clsNode

    Set cTmp = cNode.ParentNode
    Do While Not cTmp.Caption = "RootHolder"
        cTmp.Expanded = True
        Set cTmp = cTmp.ParentNode
    Loop
End Sub
but perhaps I'm way off.

In the attached demo, I used the following to expand the 3rd node below the root:

Code: Select all

        mcTree.ExpandToLevel 1
        mcTree.ExpandNode mcTree.Nodes("Root_3.1")
        mcTree.Refresh
TreeViewDemo.xlsm
PS My name is Hans, not Harris... :smile:
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans

User avatar
Jan Karel Pieterse
Microsoft MVP
Posts: 656
Joined: 24 Jan 2010, 17:51
Status: Microsoft MVP
Location: Weert, The Netherlands

Re: mctree Node Expander

Post by Jan Karel Pieterse »

I'm still not clear what you're trying to do. The expanding of nodes is already handled by the class, you whouldn't need to write any code to do that.
Regards,

Jan Karel Pieterse
Excel MVP jkp-ads.com

davidmreid
NewLounger
Posts: 12
Joined: 19 Feb 2014, 01:45

Re: mctree Node Expander

Post by davidmreid »

Hi Jan, I'm trying to open the treeview with the default view shown below. Therefore, when a Level 1 Node is selected the child nodes should also open--and all other nodes should revert to the default view. Open a second Level 1 Node and the first Level 1 node i previously opened along with any open child nodes get closed. This seems like a simple idea and I keep hacking away to find the answer. Thank you your help. BTW: Your Treeview Control is undoubtedly one of the best learning tools for understanding VB/VBA and Class Modules. -David

Client Name (Root)
View Data (Level 1)
-Select Report (Level 2)
-Choose Month (Level 2)
Export Data (Level 1)
-Choose Month (Level 2)
Import Data (Level 1)
-Select Report (Level 2)
-Choose Month (Level 2)

User avatar
Jan Karel Pieterse
Microsoft MVP
Posts: 656
Joined: 24 Jan 2010, 17:51
Status: Microsoft MVP
Location: Weert, The Netherlands

Re: mctree Node Expander

Post by Jan Karel Pieterse »

I added this to the mcTree_Click event in the userform's code module:

Code: Select all

    Dim cOtherNode As clsNode
    For Each cOtherNode In mcTree.Nodes
        If Not cNode Is cOtherNode And cOtherNode.ParentNode Is cNode.ParentNode Then
            cOtherNode.Expanded = False
        End If
    Next
    mcTree.Refresh
Regards,

Jan Karel Pieterse
Excel MVP jkp-ads.com

User avatar
Jan Karel Pieterse
Microsoft MVP
Posts: 656
Joined: 24 Jan 2010, 17:51
Status: Microsoft MVP
Location: Weert, The Netherlands

Re: mctree Node Expander

Post by Jan Karel Pieterse »

I forgot to include:

cNode.Expanded = True

just before the For-next loop...
Regards,

Jan Karel Pieterse
Excel MVP jkp-ads.com

davidmreid
NewLounger
Posts: 12
Joined: 19 Feb 2014, 01:45

Re: mctree Node Expander

Post by davidmreid »

I'll give it a try! Thank you, Jan!