Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help on treeview control

Status
Not open for further replies.

angelleynes

Programmer
Dec 28, 2001
46
US
Hello,

anyone has a sample of treeview control?

thanks!
 
Please be more specific on what it is you need. Do you need an example of how to populate the treeview? Do you need an example of how to add or remove items from the treeview? Do you need examples of how to coordinate other objects on the form with the treeview item selected?



-BP
 
sorry, basically i need to know how to use it, like adding or removing.

thanks!
 
First, I always set the treeview's LabelEdit property to 0 - Automatic. Also, I'm not using a treeview with images, just the lines with + and -.

Here's some code I have from a treeview used to define the options under a menu pad.
Code:
* The file RptMenus holds the options configured in the treeview
SELECT RptMenus
* This form allows the user to select whether the new option is above or below the currently selected item in the treeview.
* It returns the Key of the item the new item should appear under.  If there are currently no items, it returns 0.  If the user canceled the operation, it returns -1.
DO FORM forms\RptChoice WITH RptMenus.RptMenuId TO lnMenuParent
IF lnMenuParent >= 0
   lnNewId = oPCs.NextId("RptMenus")  && My routine to assign an ID
   IF lnNewId > 0
      INSERT INTO RptMenus (RptMenuId) VALUES (lnNewId)
   ENDIF 

   * Store the parent ID in the table
   replace MenuParent WITH lnMenuParent
   IF EMPTY(lnMenuParent)
      * This will become one of the top items in the treeview
      thisform.oletreeviewRptMenu.Nodes.Add("TOP",4,"RM"+TRANSFORM(RptMenus.RptMenuId),'')
   ELSE
      thisform.oletreeviewRptMenu.Nodes.Add("RM"+TRANSFORM(lnMenuParent),4,"RM"+TRANSFORM(RptMenus.RptMenuId),'')
   ENDIF
	
   * Find the index for the newly added item, and make that the selected item
   FOR lni = 1 TO thisform.oleTreeviewRptMenu.Nodes.Count
      IF "RM"+TRANSFORM(RptMenus.RptMenuId) $ thisform.oleTreeviewRptMenu.Nodes[lni].Key
         * Found the node
         thisform.oleTreeviewRptMenu.Nodes[lni].Expanded = .T.
         thisform.oleTreeviewRptMenu.selectedItem = thisform.oleTreeviewRptMenu.Nodes[lni]
         EXIT 
      ENDIF 
   ENDFOR 
   * We now want them to enter the newly added item, so let them edit the node
   thisform.oleTreeviewRptMenu.SetFocus()
   thisform.oleTreeviewRptMenu.StartLabelEdit()

Here's the code I use to remove an item.
Code:
lnCurrentId = VAL(STRTRAN(thisform.oleTreeviewRptMenu.SelectedItem.Key,"RM",""))

SELECT RptMenuId ;
	FROM RptMenus ;
	WHERE MenuParent = lnCurrentId ;
	INTO cursor TmpChildren
IF _tally > 0
   MESSAGEBOX("This option has options below it and cannot be deleted.",64,"Option in use")
   USE IN SELECT("TmpChildren")
   RETURN 
ENDIF 
USE IN SELECT("TmpChildren")

IF MESSAGEBOX("Are you sure you want to delete "+ALLTRIM(thisform.oletreeviewRptMenu.selectedItem.Text)+"?",36,"Delete request") = 6
   SELECT RptMenus
   SET ORDER to RptMenuId
   IF &quot;RM&quot;+TRANSFORM(RptMenuId) <> thisform.oleTreeviewRptMenu.SelectedItem.Key
      SEEK lnCurrentId
   ENDIF 
   IF &quot;RM&quot;+TRANSFORM(RptMenuId) = thisform.oleTreeviewRptMenu.SelectedItem.Key
      * Delete it from the table
      lnParentId = MenuParent
      DELETE
      SET ORDER to RptMenuId
      SEEK lnParentId
      * Remove it from the treeview
      thisform.oleTreeviewRptMenu.Nodes.Remove(thisform.oleTreeviewRptMenu.selectedItem.Index)
      thisform.Expandtreeview()
      thisform.oleTreeviewRptMenu.Refresh()
   ENDIF
ENDIF



-BP
 
Assumming you've installed the sample code with your VFP, versions 6->8.0 all have code for this in the SOLUTION.APP. Try:
Code:
do (_samples+&quot;\solution\solution.app&quot;)
The first sample under ActiveX -> ActiveX Controls is &quot;Add and remove items in a treeview control&quot;.

Doug Hennig of Stonefield also has some sample code in his &quot;Technical Papers&quot; (at - see &quot;Using Visual FoxPro ActiveX Controls (118K)&quot;.

Rick
 
Here's a quick example. It just builds nodes. You can then remove a node by double-clicking the node.

Drop a treeview control on your form named oletreeview1. Drop a command button on your form and add the following code to the Click event:
Code:
WITH THISFORM.oletreeview1
   FOR z = 1 TO 5
      STORE 'znode' + ALLTRIM(STR(z)) TO znode
      .nodes.ADD(,,znode, znode)

      FOR j = 1 TO 5
         STORE 'jnode' + ALLTRIM(STR(j)) TO jnode
         .nodes.ADD(znode, 4, znode + jnode , jnode)

         FOR k = 1 TO 5
            STORE 'knode' + ALLTRIM(STR(k)) TO knode
            .nodes.ADD(znode + jnode, 4, znode + jnode + knode, knode)

            FOR l = 1 TO 5
               STORE 'lnode' + ALLTRIM(STR(l)) TO lnode
               .nodes.ADD(znode + jnode + knode, 4, znode + jnode + knode + lnode, lnode)
            NEXT
         NEXT
      NEXT
   NEXT
ENDWITH

RETURN


Add this code to the DoubleClick event of the treeview control:
Code:
This.nodes.remove(This.SelectedItem.Key)

The important thing to note about the treeviw control, is that node id's must be unique. Once you get over that hurdle, the rest ain't so bad. That tripped me up a little in the beginning. One of the easiest ways I found to keep them straight was to keep track of which 'column' the node was, as in the above example.



-Dave S.-
[cheers]
Even more Fox stuff at:
 
Look at C:\%WIN%\Help\CMCTL198.CHM - help & docu for TreeView and others controls. (%WIN% for WINNT or WINDOWS)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top