Hello,
I´m not a professional in vb.net, but i try to make a tool for my colleague and me. I got a XMLTreeView on my form and i load a XML-File in it. Now, I want to set each node in the XMLTreeView another icon, but it must set by the XML-File.
This is my XML-File :
<?xml version="1.0" encoding="utf-8" ?> <Nodes> <Node Text="Programme" Icon="BlaBlub.ico"> <Node Text="Test" Icon="C:\MyProgramm\Icons\icon2.ico"> <Programm Path="C:\Program Files (x86)\Microsoft Visual" & _
"Studio\2017\Enterprise\Common7\IDE\devenv.exe">Visual Studio</Programm> <Programm Path="C:\Program Files (x86)\Microsoft" & _
"Office\root\Office16\EXCEL.EXE">Excel</Programm> </Node> <Node Text="andere" Icon="C:\MyProgramm\Icons\23.ico"> <Programm Path="C:\Program Files (x86)\Microsoft Visual" & _
"Studio\VB98\VB6.EXE">VB.NET</Programm> </Node> <Node Text="Test1" Icon="Huhu.ico"> <Programm>bla</Programm> <Programm>blup</Programm> </Node> </Node> </Nodes>
This is my vb.net code :
Option Explicit On
Option Strict On
Imports System
Imports System.Net
Imports System.Net.Sockets
Imports System.Runtime.InteropServices
Imports System.Net.NetworkInformation
Imports System.IO
Imports System.Text
Imports System.Diagnostics
Imports System.ComponentModel
Imports System.Drawing
Imports System.Management
Imports Microsoft.Win32
Imports System.Threading
Imports System.Xml
Imports System.Collections.Generic
Imports System.Windows.Forms
Public Class XmlTreeView
Public Class MyTreeNode
Inherits TreeNode
Public Property Path As String
End Class
Public Shared Sub LoadFromXml(ByVal FileName As String, ByVal TheTreeView As TreeView)
Dim xDoc As New XmlDocument
xDoc.Load(FileName)
FillTreeView(TheTreeView.Nodes, xDoc.DocumentElement)
End Sub
Private Shared Sub FillTreeView(ByVal CurrentNodes As TreeNodeCollection, ByVal xNode As XmlNode)
For Each xChild As XmlNode In xNode.ChildNodes
If xChild.Name <> "Programm" Then
FillTreeView(CurrentNodes.Add(xChild.Name).Nodes, xChild)
Else
CurrentNodes.Add(New MyTreeNode With {.Text = xChild.InnerText, .Path = xChild.Attributes("Path").Value})
End If
Next
End Sub
Sub treeView1_NodeMouseDoubleClick(ByVal sender As Object, ByVal e As TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseDoubleClick
Try
Dim path = TryCast(TreeView1.SelectedNode, MyTreeNode).Path
If path IsNot Nothing Then Process.Start(path)
Catch ex As Exception
MsgBox("kein Programm gefunden")
End Try
Select Case e.Node.Text
Case "Visual Studio"
MsgBox("Visual Studio")
'...
Case "Excel"
MsgBox("Excel")
'...
End Select
End Sub
Private Sub XmlTreeView_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Controls.AddRange(New Control() {TreeView1})
Me.TreeView1.Nodes.Clear()
XmlTreeView.LoadFromXml("nodes - kopie.xml", Me.TreeView1)
'TreeView1.Nodes(0).Expand()
End Sub
End Class
I created on my form an ImageList too.
How can i set the icon for each node? I think it has to be dynamically. But I don´t know no how to do this... (I don´t want to load all Icons to the ImageList manually)
I hope you can give me some code, please.