Friday, 8 April 2016

How To Select All Nodes In Treeview In VFP 9.0


A more appropriate title of this article should have been  "How To Check All Child Nodes From a Parent Node In Treeview" but since I used a treeview control that contained only one parent node with multiple child nodes within, I guess I'd stick with the current title.

Here's one way I achieve the effect. But before I show the code behind, take note of the underlying table records where the treeview gets its record from.


The parent node has a "02_" string as a key as shown from the highlighted row in the image above. All the child nodes have different keys but have the same parent, a "02_" text that is used as a pointer to their parent node.

In the Treeview's .NodeCheck event, I wrote the following codes.

*** ActiveX Control Event ***
Lparameters Node
If Node.Key <> '02_'
      Return
Endif

For Each oNode In Thisform.oletreeView.Nodes
      oNode.Checked = Iif(Node.Checked,.F.,.T.)
Endfor

You can see that I added a checking routine to know if the user is ticking on the parent node or the child node. If the user has checked any of the child node, it will stop from there and will not proceed to executing the next codes, hence the "Return" keyword.

Next, I reiterate through all the treeview's nodes and set each node's checked property to either TRUE or FALSE depending on the parent's current "checked " status. If the parent's checkbox is unticked, the current child node in focus during which is set to checked; otherwise, it is unchecked.

Here's the result of which.


3 comments:

  1. hi sir. Do you know where i can download a free foxpro treeview class like yours? thanks

    ReplyDelete
  2. Hello,
    I have a tree control that uses recursion to lookup data in the database to create child nodes and at the same time set them to Checked when multiple selection of nodes is enabled.
    The code looks like the sample provided on another thread with similar title to this one. Basically: TREE.CHECKNODE event calls a custom method in its parent container/form, and this method calls itself - code shown below.

    We apply the same logic to several tables that have the same hierarchy.
    However, we recently encountered an issue with a new table that follows the same hierarchy as the other ones.
    We hit the Insufficient Stack Space message for one particular set of codes in the new table.
    We already tried setting the STACKSIZE value to 64000 in the config.fpw file, but that did not help.

    Could anyone provide any other suggestions on how to address this issue?

    Thank you for any help you can provide.

    Regards,
    Patricia Cu

    *** OLE Control CHECKNODE Event ***
    LPARAMETERS node
    local lval
    thisform.startwait
    lval = node.checked
    with this.parent.parent
    if type('node.child.key') <> 'U'
    .checknodes(node.child,lval)
    endif
    if lval
    .btnok.enabled = .t.
    endif
    endwith
    thisform.endwait
    node = .f.


    *******parent.parent container CHECKNODES method*****************
    lpara node, lval
    this.gtree1.treecontrol.expand(node.parent)
    node.checked = lval
    if type('node.child.key') <> 'U'
    this.checknodes(node.child,lval)
    endif
    if type('node.next.key') <> 'U'
    this.checknodes(node.next,lval)
    endif
    node = .f.


    ********parent.parent container EXPAND method********
    **setup sql statement
    **run sql statement and create nodes with the resulting cursor
    **also set attributes for the nodes

    with this.nodes
    ogx.odbm.execsql(thisform.datasessionid,sSql,'branch')
    msele = sele()
    select('branch')
    scan
    if dispcode = code
    cTitle = nvl(alltrim(dispcode),' ')+' '+alltrim(desc1)+' '+alltrim(str(lev))
    else
    cTitle = substr(alltrim(own),1,len(alltrim(own)))+' '+nvl(alltrim(dispcode),' ')+' '+alltrim(desc1)+' '+alltrim(str(lev))
    endif
    mnode = .Add(mRK+own,4,mRK+code, alltrim(cTitle))
    mnode.bold = iif(dtflg = 1 and this.parent.parent.seltyp<3,.t.,.f.)
    if type('mnode.tag') = "C"
    if this.parent.parent.seltyp > 2
    if dtflg = 1
    mnode.tag = substr(alltrim(own),1,len(alltrim(own)))
    else
    mnode.tag = dispcode
    endif
    else
    if dtflg = 1
    mnode.tag = dispcode
    else
    mnode.tag = ""
    endif
    endif
    endif
    endscan
    select(msele)
    endwith
    mnode = .f.

    ReplyDelete
  3. Hello,
    I have a tree control that uses recursion to lookup data in the database to create child nodes and at the same time set them to Checked when multiple selection of nodes is enabled.
    The code looks like the sample provided on another thread with similar title to this one. Basically: TREE.CHECKNODE event calls a custom method in its parent container/form, and this method calls itself - code shown below.

    We apply the same logic to several tables that have the same hierarchy.
    However, we recently encountered an issue with a new table that follows the same hierarchy as the other ones.
    We hit the Insufficient Stack Space message for one particular set of codes in the new table.
    We already tried setting the STACKSIZE value to 64000 in the config.fpw file, but that did not help.

    Could anyone provide any other suggestions on how to address this issue?

    Thank you for any help you can provide.

    Regards,
    Patricia Cu

    *** OLE Control CHECKNODE Event ***
    LPARAMETERS node
    local lval
    thisform.startwait
    lval = node.checked
    with this.parent.parent
    if type('node.child.key') <> 'U'
    .checknodes(node.child,lval)
    endif
    if lval
    .btnok.enabled = .t.
    endif
    endwith
    thisform.endwait
    node = .f.


    *******parent.parent container CHECKNODES method*****************
    lpara node, lval
    this.gtree1.treecontrol.expand(node.parent)
    node.checked = lval
    if type('node.child.key') <> 'U'
    this.checknodes(node.child,lval)
    endif
    if type('node.next.key') <> 'U'
    this.checknodes(node.next,lval)
    endif
    node = .f.


    ********parent.parent container EXPAND method********
    **setup sql statement
    **run sql statement and create nodes with the resulting cursor
    **also set attributes for the nodes

    with this.nodes
    ogx.odbm.execsql(thisform.datasessionid,sSql,'branch')
    msele = sele()
    select('branch')
    scan
    if dispcode = code
    cTitle = nvl(alltrim(dispcode),' ')+' '+alltrim(desc1)+' '+alltrim(str(lev))
    else
    cTitle = substr(alltrim(own),1,len(alltrim(own)))+' '+nvl(alltrim(dispcode),' ')+' '+alltrim(desc1)+' '+alltrim(str(lev))
    endif
    mnode = .Add(mRK+own,4,mRK+code, alltrim(cTitle))
    mnode.bold = iif(dtflg = 1 and this.parent.parent.seltyp<3,.t.,.f.)
    if type('mnode.tag') = "C"
    if this.parent.parent.seltyp > 2
    if dtflg = 1
    mnode.tag = substr(alltrim(own),1,len(alltrim(own)))
    else
    mnode.tag = dispcode
    endif
    else
    if dtflg = 1
    mnode.tag = dispcode
    else
    mnode.tag = ""
    endif
    endif
    endif
    endscan
    select(msele)
    endwith
    mnode = .f.

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...