Library tutorials & articles
TreeView Control
Using Images
Like the listview control, the treeview control needs to be 'bound' to an image control. To do this, add an ImageList Control to your form, name it, and add any images you want to use in the TreeView control to it. Be sure to set the image size first (normally set to 16x16). Set keys if you wish, as this makes it easier to identify each image. It is best to stick with one case system (ie all caps, all lowercase etc).
Now, change the ImageList property of the TreeView control to the name of the ImageList you want to use. Now, when you add a new node to the control, you can specify and image index or key too. That image will then be displayed next to the node. The following code adds a new node, with the image "TextFile" displayed next to it.
TreeView1.Nodes.Add , , , "TestImageItem",
"TextFile"
You can also specify a image to be displayed when the item is selected . The following code adds a new node. The image "FolderClosed" will be displayed, and when it is selected, "FolderOpen" will be displayed (like the folders in explorer).
TreeView1.Nodes.Add , , , "TestFolderItem",
"FolderClosed", "FolderOpen"
Note that in place of the text used to identify the image, you can also use its index:
TreeView1.Nodes.Add , , , "TestImageItem", 1
Related articles
Related discussion
-
VB6 Runtime error 381 subsript out of range Error
by Uncle (2 replies)
-
passing and reading parameters from using Shell
by jigartoliya (0 replies)
-
Convert C++ code to VB6
by mawcot (4 replies)
-
listbox scrollbar
by Dennijr (10 replies)
-
Can you describe Above simple VB6 code?
by pramodmca09 (0 replies)
Related podcasts
-
Christian Beauclair
14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...
I tried your treeview example and when I try to add and image it is telling me that I need to initilaize the image first. Can you tell me what that means and how to fix it
I tried this and recieved the following: Compile error: variable not defined..."TreeView" is highlited...anyone have any ideas?
gabriel medina
gxsoft@hotmail.com
http://www.pldental.com
Hi,
Found this very good tutorial because its nearly exact the problem i have with my TreeView,...
...but unfortunatly, i can't get it to work...
Set the same settings like in the tutorial, but nothing happens.
I try to drag and drop, but it seems, there is no action by the script,...
Any suggetions, what my failure could be?
Thanx for help
LittleMik
Enjoyed the piece on drag and drop treeview.
I use VS6 under XP and in that environment the DataObject declaration is
Data As MSComctl2.DataObject
rather than the reference in the posting
Hyperlink to articles on DataSource and imagelist would be handy but they are both documented in MSDN if you throw the rigth incantation at it.
Nigel
Just what i needed for a contact list im doing for my MSN Clone. Excellent!
Can this be implemented using ASP pages as well?
Thanks.
I hope someOne found this useful
Private Sub Form_Load()
With TreeView1
.Style = tvwTreelinesPlusMinusPictureText
.LineStyle = tvwRootLines
.PathSeparator = "\"
.Indentation = Screen.TwipsPerPixelX * 5 '256
.LabelEdit = tvwManual
.SingleSel = False
.HideSelection = False
.ImageList = ImageList1
.Refresh
End With
Call ShowEstructure
End Sub
Private Sub ShowEstructure()
Dim i As Byte
Dim j As Byte
Dim k As Byte
Dim Max As Byte
Dim sp As String
Dim sh As String
Dim sh2 As String
Dim TotalSecciones As Byte
Dim TotalPosiciones As Byte
Dim rsSeccions As New ADODB.Recordset
Dim rsPositions As New ADODB.Recordset
TreeView1.Nodes.Clear
Set grsGral = New ADODB.Recordset
grsGral.Open "EXEC spSQL_ShowDepartments", gConnectionString, adOpenStatic, adLockReadOnly, 1
Max = grsGral.RecordCount
For i = 1 To Max
'Actual Node
sp = "nodo" & CStr(i)
TreeView1.Nodes.Add , , sp, grsGral.Fields(1).Value, 1
grsGral.MoveNext
Next
grsGral.MoveFirst
For i = 1 To Max
' leaf
sp = "nodo" & CStr(i)
Set rsSeccions = New ADODB.Recordset
rsSeccions.Open "EXEC spSQL_ShowSeccions_forThis '" & grsGral.Fields(0).Value & "'", gConnectionString, adOpenStatic, adLockReadOnly, 1
TotalSecciones = rsSeccions.RecordCount
For j = 1 To TotalSecciones
' relative son
sh = sp & "-" & CStr(j) 'j
TreeView1.Nodes.Add sp, tvwChild, sh, rsSeccions.Fields(0).Value, 2
'*-*-*-
Set rsPositions = New ADODB.Recordset
rsPositions.Open "EXEC spSQL_ShowPositions_forThis '" & rsSeccions.Fields(1).Value & "'", gConnectionString, adOpenStatic, adLockReadOnly, 1
TotalPosiciones = rsPositions.RecordCount
For k = 1 To TotalPosiciones
' La clave del nodo hijo, relativo al nodo sP
sh2 = sh & "-" & CStr(k) 'j
TreeView1.Nodes.Add sh, tvwChild, sh2, rsPositions.Fields(0).Value, 3
rsPositions.MoveNext
Next
Set rsPositions = Nothing
'*--*-*-
rsSeccions.MoveNext
Next
Set rsSeccions = Nothing
grsGral.MoveNext
Next
Set grsGral = Nothing
End Sub
Hoped to find some example using directories and files...
Sorry, I went back and read it more closely. I just had to play around with the ImageList control to make it work.
I might have missed it, but I would have liked to have seen how to use images without having to go to the imagelist tutorial. I know you mentioned imagelist, but perhaps a brief explanation of using the imagelist control with TreeView would have been in order. If I missed it, I apologize. Thank you for a very helpful tutorial.
This thread is for discussions of TreeView Control.