Hope this is straightforward enough.....it semonstrates adding the root, 2 child nodes, 2
nodes under each child, and another uner the last child. It aslo shows using both strings
hard coded and as variables as the keys.
In general, the Add method uses this syntax:
nodX = TreeView1.Nodes.Add(parent's key name, _
relationship constant ie tvwChild, _
this key's keyname, _
this items caption, _
index to icon when, _
index to icon if selected)
To add an item, you specify the parent key, the relationship, the key for this item if it
is going to have sub-items (because this becomes the patent key for those sub-items),
obviously the text to display, and optionally the icon indexes corresponding to an
imagelist on the form containing 16x16 images. A new "root" item has no parent, so commas
delimit the unneeded data.
Undoubtedly, the hardest thing is to create unique keynames for each item added. Below,
in this simple example, I've hard-coded the values. But realistically, you will want to
vreate those on-the-fly, using a counter. Note however that a key must contain at least 1
thext charcter...numbers alone are not enough, and will generate and error.
Private Sub Command2_Click()
Dim nodX As Node
Dim cText, cKey, spkey1, mpkey, TreeName, pKey
TreeView1.Nodes.Clear
'create the text for the root tree item & expand the root node
TreeName = "Web pages"
pKey = "root"
Set nodX = TreeView1.Nodes.Add(, , pKey, TreeName, 1, 1)
nodX.Expanded = True
'-----------------
'add children as a sibling of parent "Web pages"
cText = "My Pages"
cKey = "key1"
Set nodX = TreeView1.Nodes.Add(pKey, tvwChild, cKey, cText, 1, 1)
nodX.Expanded = True
cText = "Your Pages"
cKey = "key2"
Set nodX = TreeView1.Nodes.Add(pKey, tvwChild, cKey, cText, 1, 1)
nodX.Expanded = True
'-----------------
'add some children under "My Pages"
cText = "My Page's Home page"
mpkey = "mpkey1"
Set nodX = TreeView1.Nodes.Add("key1", tvwChild, mpkey, cText, 1, 1)
nodX.Expanded = True
cText = "My Page's Search page"
mpkey = "mpkey2"
Set nodX = TreeView1.Nodes.Add("key1", tvwChild, mpkey, cText, 1, 1)
nodX.Expanded = True
'add some children under "Search page"
cText = "My Page's Search Results Form"
spkey1 = "spkey1"
Set nodX = TreeView1.Nodes.Add("mpkey2", tvwChild, spkey1, cText, 1, 1)
nodX.Expanded = True
'-----------------
'add some children under "My Pages"
cText = "Your Page's Home page"
mpkey = "ypkey1"
Set nodX = TreeView1.Nodes.Add("key2", tvwChild, mpkey, cText, 1, 1)
nodX.Expanded = True
cText = "Your Page's Search page"
mpkey = "ypkey2"
Set nodX = TreeView1.Nodes.Add("key2", tvwChild, mpkey, cText, 1, 1)
nodX.Expanded = True
'add some children under "Search page"
cText = "Your Page's Search Results Form"
spkey1 = "yspkey1"
Set nodX = TreeView1.Nodes.Add("ypkey2", tvwChild, spkey1, cText, 1, 1)
nodX.Expanded = True
End Sub
--
Randy Birch, MVP Visual Basic
VBnet, The Visual Basic Developers Resource Centre
http://home.sprynet.com/sprynet/rasanen/vbnet/default.htm
Common Controls Replacement Project
http://www.mvps.org/ccrp
:Hi,,,I was wondering if there is a so called "simple" "easy"
:treeview....What I am trying to say is...Is there a simple way to code the
:stuff in it?
:
:If so could someone help me?
:
:Like I want it to look like this...
:
:Home
:- Web Pages
: My page
: Your page
:
:
:Can that be done simply? (BTW once I figure it out...I will know how to do
:the rest... I used the web page stuff because I couldn't think of anything
:else..Thanks!
:
: