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

TreeView Control???

Status
Not open for further replies.

jgoodman00

Programmer
Jan 23, 2001
1,510
Hi,

Has anybody had any dealings with this 'phenomenon', because the help file seems a little confusing on how yu actually set it up. We would like to use this object to display a hierarchical list of items from a source table (which is already created). We would then like it to be possible to navigate through the tree to select any given item for which data is required. I have managed to insert images (using an ImageList control), but obviously we want to insert text objects....

Can anyone tell me how we can go about achieving this, or if you have any better ideas please..... James Goodman
j.goodman00@btinternet.com
 
If you are trying to have the tree show the actual text I have not seen that done before. However the way it usually works is like in explorer you show the name of the text file in the tree then when you click on it it either opens the text file in another window or in a textbox or such on the form.
If you are just unsure of how to load the nodes here is a quick "sloppy" example. Just be sure the object is named treeview1.
Private Sub Form_Load()
treeview1.Nodes.Add , , "firstkey", "first"
treeview1.Nodes.Add , , "secondkey", "second"
treeview1.Nodes.Add "firstkey", tvwChild, "childkey", "child"
treeview1.Nodes.Add "childkey", tvwChild, "child2key", "CHILD2'"
treeview1.Nodes.Add "secondkey", tvwChild, "workkey", "Anotherrow"
treeview1.Nodes.Add "child2key", tvwChild, "thirdkey", "thirdrow"
End Sub
There are much nicer ways of doing this but this gives an example of how the nodes work.
If you are beyond this I apoligize for the redundancy.
 
I quickly nipped this together from existing stock...read carefully and contact me if you need help to stack deeper than this single node level. Use Cstr()and some text (if using numbers) to name your node keys or you will get invalid key errors(known fault).
Gord

Public Sub BuildTree()
On Error GoTo ErrBT
Dim ctltree As Control, ctlImage As Control, NodeA As Node, Db As Database, Rs As Recordset, SQL As String
Set frm = Forms!YourFormsName
Set ctltree = frm.YourTreesName
Set ctlImage = frm.YourImageListsName
ctltree.ImageList = ctlImage.Object
ctltree.LineStyle = tvwRootLines
Set NodeA = ctltree.Nodes.Add(, , "RN", "This is the root", YourImageNo, YourSelectedImageNoIfUsed)
Set Db = CurrentDb()
SQL = "Select YourTable.* From YourTable"
Set Rs = Db.OpenRecordset(SQL, dbOpenSnapshot, dbForwardOnly)
Do Until Rs.EOF
Set NodeA = ctltree.Nodes.Add("RN", 4, YourKeyNameShouldbeAString, TheTextYouWantToDisplay, ImageNo, SelectedImageNo)
Rs.MoveNext
Loop
Rs.Close

ExitBT:
Exit Sub

ErrBT:
MsgBox Err.Number & " " & Err.Description, vbInformation, "Tree build error."
Resume ExitBT
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top