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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TreeView in a ListBox

Status
Not open for further replies.

neculai

Programmer
Jul 21, 2004
13
CA
I would like to know if it is possible to use TreeView control inside a ListBox. I have populated a ListBox with few items, using AddItem(), and one of the Items I would like to have the same behaviour like a TreeView. I don't have experience with TreeView at all, but I want to know if this thing can be done. If so, I'll start digging deep, I understand that this control is not very easy to work with.
Thank you in advance,
Neculai
 
No, the listbox cannot host other controls.

You could use a treeview INSTEAD of a listbox, though, and just don't populate the nodes on those rows that don't have any.
 
No, it'd definitely not possible to have a treeview inside a listbox. What you can do (probably) is have a treeview inside a grid, but I wouldn't recommend it.

If your aim is to have a control where some of the items can expand in a tree-like way, then just use a treeview on its own. It does (almost) everything a listbox can do, and is not all that difficult to work with.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Thank you, Mike and Dan. I've used a ListBox with few items, and I did the TreeView Like behaviour (expanding from a node), but I couldn't do the reverse (once expanded, I couldn't do the return to the initial state). The graphics is not exactly like in TreeView node expanding, but the function is the same, when I click on a [+] located in front of one item, I am able to display few more options for that node (I don't display those branches). I did all these using ListBox properties and methods. I just want to do it more appalling and to be able to do the return to the initial state. I will work on it.
Have a nice day!
Neculai :)
 
Why the effort, you can let a treeview have all nodes, that shuold look like normal listbox items as root nodes with no child nodes and the items you want expandable as root nodes with child nodes. Then that's exactly having the look you intend to have with the modified listbox.

Bye, Olaf.

 
I presume this is for educational purposes.
A node can be expanded or collapsed.
A node can be visible or hidden.
There are two cursors: the one with all nodes, no matter their state (expanded, visible, etc.) and the second cursor extracts only the visible nodes.

Here is a proposal, derived from a very similar one created for a grid, here : Link
Have fun [smile]

Code:
PUBLIC ofrm
ofrm=CREATEOBJECT("MyForm")
ofrm.show()

DEFINE CLASS MyForm as Form
	ADD OBJECT lst as MyList
	PROCEDURE init 
		CREATE CURSOR table1(country_id n(3),country c(12),city_id n(3),city c(12),contact_id n(3),contact c(12))
			INSERT INTO table1 values(25,'IRAN',44,'TEHRAN',6,'ALI')
			INSERT INTO table1 values(25,'IRAN',44,'TEHRAN',9,'ZAHID')
			INSERT INTO table1 values(25,'IRAN',444,'QUM',1,'AKBAR')
			INSERT INTO table1 values(184,'TURKEY',6,'ANKARA',3,'CETIN')
			INSERT INTO table1 values(184,'TURKEY',6,'ANKARA',46,'BORIS')
			INSERT INTO table1 values(184,'TURKEY',6,'ANKARA',8,'TAYYAB')
			INSERT INTO table1 values(184,'TURKEY',7,'ISTANBUL',6,'MARYAM')
			INSERT INTO table1 values(184,'TURKEY',7,'ISTANBUL',730,'JOSEPH')
			INSERT INTO table1 values(184,'TURKEY',7,'ISTANBUL',9,'AHMED')
		This.lst.recordsource='table1'
		This.lst.Initlst
	ENDPROC
ENDDEFINE

DEFINE CLASS MyList as Listbox
	RowSourceType=3 && sql expression
	RowSource=''
	width=200
	recordsource=Null && initial cursor (test1)
	allcursor=SYS(2015) && cursor with all columns
	visiblecursor=SYS(2015) && cursor with visible columns
	PROCEDURE Initlst
		LOCAL lcS
		CREATE CURSOR l1 (country_id n(3),country c(12),city_id n(3),city c(12),contact_id n(3),contact c(12),cky C(10) DEFAULT SYS(2015),pky C(10) Null,nlvl I DEFAULT 1)
		SELECT l1
		INDEX on country TAG country
		INSERT INTO l1 (country_id,country) SELECT distinct country_id,country FROM (This.recordsource) table1 

		CREATE CURSOR l2 (country_id n(3),country c(12),city_id n(3),city c(12),contact_id n(3),contact c(12),cky C(10) DEFAULT SYS(2015),pky C(10) Null,nlvl I DEFAULT 2)
		SELECT l2
		INDEX on country TAG country
		INDEX on city TAG city
		INSERT INTO l2 (country_id,country,city_id,city,pky) SELECT distinct t.country_id,t.country,t.city_id,t.city,l1.cky as pky FROM (This.recordsource) t,l1 WHERE t.country_id=l1.country_id


		CREATE CURSOR (This.allcursor) (nIdNo I AUTOINC,country_id n(3),country c(12),city_id n(3),city c(12),contact_id n(3),contact c(12),cky C(10) DEFAULT SYS(2015),pky C(10) Null,nlvl I DEFAULT 3,lVis L,lExp L)

		INSERT INTO (This.allcursor) (country_id,country,city_id,city,contact_id,contact,cky,pky,nlvl,lVis,lExp) ;
			SELECT l1.*,.T. as lVis,.T. as lExp FROM l1 UNION ALL ;
			SELECT l2.*,.T. as lVis,.T. as lExp FROM l2 UNION ALL ;
			SELECT table1.*,SYS(2015) as cky,l2.cky as pky,3 as nlvl,.T. as lVis,.T. as lExp FROM (This.recordsource) table1,l2 ;
			WHERE table1.country_id=l2.country_id AND table1.city_id=l2.city_id ORDER BY 2,4 
			
		lcS="SELECT SPACE(nlvl*4)+IIF(lExp,'[-] ','[+] ')+EVL(contact,EVL(city,country)),cky,nlvl,lExp FROM "+(This.allcursor)+" WHERE lVis ORDER BY country_id into cursor "+This.visiblecursor+" READWRITE"
		This.ColumnCount=1
		
		This.RowSource=lcS
	ENDPROC
* Expand / collapse feature	
	PROCEDURE click
		SELECT (This.visiblecursor)
		LOCAL lcS,lnR,ccky
		lnR=RECNO()
		ccky=cky
		IF lexp && collapse
			This.collapse(m.ccky)
			UPDATE (This.allcursor) SET lexp=.F.,lVis=.T. WHERE cky=m.ccky
		ELSE
			This.expand(m.ccky)
		ENDIF
		This.requery
		SELECT (This.visiblecursor)
		GO MIN(lnR,RECCOUNT(This.visiblecursor))
	ENDPROC
	PROCEDURE expand
		LPARAMETERS cky
		LOCAL lnRec
		SELECT (This.allcursor)
		lnRec=RECNO()
		UPDATE (This.allcursor) SET lvis=.T.,lexp=.F. WHERE pky=m.cky
		UPDATE (This.allcursor) SET lvis=.T.,lexp=.T. WHERE cky=m.cky
		GO lnRec
		replace lexp WITH .T.
	ENDPROC
	PROCEDURE collapse
		LPARAMETERS cky
		LOCAL c1
		c1=SYS(2015)
		UPDATE (This.allcursor) SET lexp=.F.,lVis=.F. WHERE pky=m.cky
		SELECT * FROM (This.allcursor) WHERE pky=m.cky INTO CURSOR (c1)
		IF RECCOUNT(c1)>0 AND !ISNULL(&c1..cky)
			SELECT (c1)
			SCAN
				This.collapse(&c1..cky)
				SELECT (c1)
			ENDSCAN
		ENDIF
ENDDEFINE

Respectfully,
Vilhelm-Ion Praisach
Resita, Romania
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top