Hyperlinked Treeview

gladiator83x

Member
Joined
Jan 15, 2008
Messages
11
Programming Experience
Beginner
Hi All,

I am trying to make every child node in my treeview a hyperlink. I changed to "HotTracking" property from False to true, but that just underlined every child node which contains a file name, and it does not allow me to open the file. I was wondering if someone could point me in the right direction. I am using the code below. Thanks a bunch.


Dim folders2 As New List(Of String)

folders2.Add("2.0 Test Setup")

For Each folder2 As String In folders2

Dim dir2 As New IO.DirectoryInfo(folder2)

Dim node2 As TreeNode = Me.TreeView1.Nodes.Add(dir2.Name)

For Each file2 As IO.FileInfo In dir2.GetFiles

node2.Nodes.Add(file2.Name & " .................... " & file2.LastWriteTime)

Next
Next

 
You'd have to handle the appropriate event so that when a node is clicked you can determine which node, get the appropriate data from that node and then use that data appropriately. For a start that means you need to store the full path along with each node. Once you've got that path you can call Process.Start to open it in its default application.

What events does the TreeView control provide? If you don't know then go to the MSDN documentation and read about it.
 
Hi,

My main problem is that I am trying to display hyperlinked child nodes (file names) within a treeview. I am taking jmcilhinney's advice and I am trying to first display the file paths then store the path, but it seems that I cannot convert between system io file type variables and strings so that I can view the paths within my treeview. I know that I am close b/c I can already display the filenames, but in string format.

I am able to generate a treeview that contains a root (directory As a sting) and the child nodes (files within the directory--As strings). I was trying to add this code and code similar to this, but I still keep running into errors.

Dim FileName As IO.DirectoryInfo
FileName = Application.CurrentProject.Path
TextBox1.Text = FileName


The errors state:
(i)
'CurrentProject' is not a member 'System.Windows.Forms.Application'.C:...
(ii)
Value of type 'System.IO.DirectoryInfo' cannot be converted to 'String'.C:..

I will greatly appreciate any type of direction. Thanks.
 
With the level 0 nodes as 'directories' and level 1 nodes as 'files' you already have the path, it's the parent node, but this would need to be the FullName and not just Name (for example "c:\dir1" instead of "dir1"). Combine the path and filename with IO.Path.Combine method. You probably have found the NodeMouseClick event by now, where you can access the click node from e.Node. You'd also be interested in knowing that the node level information is available to read from the TreeNode, it has a Level property :) Put these three bits together and the solution is ready, code in NodeMouseClick event handler:
VB.NET:
Expand Collapse Copy
If e.Node.Level = 1 Then
    Process.Start(IO.Path.Combine(e.Node.Parent.Text, e.Node.Text))
End If
Another option if you really don't want to display full path for directories, in directory TreeNode store the FullName in Tag property:
VB.NET:
Expand Collapse Copy
node.Tag = dir.FullName
So in NodeMouseClick event handler you would then simply do this to start the process:
VB.NET:
Expand Collapse Copy
Process.Start(IO.Path.Combine(e.Node.Parent.Tag, e.Node.Text))
 
Last edited:
With the help of both JohnH and jmcilhinney, I am coming along in my small project. However, it seems when I overcome one problem, another arises.

Program Description. I click on a button, and a treeview comes up containing a list of directories, all of which contain files themselves. It is difficult to perform treeview functions, while programming within the click button subfunction itself.

Problem:

VB.NET:
Expand Collapse Copy
Dim folders2 As New List(Of String)

folders2.Add("C:\\2.0 Test Setup")

For Each fileDir2 As String In folders2
    x=fileDir2

    Dim direct2 As New IO.DirectoryInfo(fileDir2)
    Dim node2 As TreeNode = Me.TreeView1.Nodes.Add(direct2.Name)

    For Each file2 As IO.FileInfo In direct2.GetFiles
        node2.Nodes.Add(file2.Name & " .................... " & file2.LastWriteTime)

        Dim y = IO.Path.Combine(x, file2.Name)

        TextBox1.Text = y
        ' Process.Start(y)
    Next
Next

My variable file2 lists all the file names under a directory. It is inside a for loop. I tried making file2 and array, but ran into system errors.

I was able to obtain the correct path, but when I opened up the file using process.start, all the files were opened.

Questions:

Is there anyway I can make file2 an array?

Are global variables allowed in VB, can I pass a value from sub-function to subfunction?

And finally, How would I enable the click button option for nodes in my treeview if I am doing all my programming under the click-button subfunction?

Thanks.
 
Last edited by a moderator:
Back
Top