0

I don´t know why, but it still show the node names like "visualstudio". normaly it shows only the nodes-text like "visual studio" (space between the words).
tree3105.jpg

this is the xml :

<?xml version="1.0" encoding="UTF-8"?>
<Programme>
  <Feld Icon="Folder.ico">Feld 1<VisualStudio Path="C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\devenv.exe" Icon="VS.png">Visual Studio</VisualStudio>
    <Excel Path="C:\Program Files\Microsoft Office 15\root\office15\EXCEL.EXE" Icon="Excel.png">Excel</Excel>
  </Feld>
  <Home Icon="Folder.ico">Home Place<Notepad Path="C:\Program Files\Notepad++\notepad++.exe" Icon="notepad.png">Notepad ++</Notepad>
  </Home>
</Programme>
0

Sorry, I can't understand what's wrong now. Could you explain more? Your last image looks like what you were after. In any case, by now can't you resolve it by yourself?

0

In the screenshot you can see the nodes. to explain my problem, look (for example) only to the node "VisualStudio Visual Studio".

The first"VisualStudio" (without empty space between the 2 words) = node-name in the xml-file.
The second "Visual Studio" (with empty space between the 2 words) = node-text in the xml-file.

Normaly you can only see the node-text in treeviews.

For more example maybe this : if i create the nodes manualy in the form, it shows ONLY the node.text
Tree_Create.png

I´m sorry, but i can´t solve it by myself, because i don´t know how...

Edited by josh_4

1

treeView4.PNG
Let's see how it goes this time. I've left commented the old instruction to visualize better the change.

    Private Sub AddNode(ByVal inXmlNode As XmlNode, ByVal inTreeNode As TreeNode)
        Dim i As Integer
        If inXmlNode.HasChildNodes Then
            Dim nodeList As XmlNodeList
            nodeList = inXmlNode.ChildNodes
            i = 0
            While i <= nodeList.Count - 1
                Dim xNode As XmlNode = inXmlNode.ChildNodes(i)
                Dim imgIndex As Int32 = -1
                If xNode.Attributes IsNot Nothing Then
                    For Each att As XmlAttribute In xNode.Attributes
                        If att.Name = "Icon" Then
                            imgIndex = addImage(att.Value)
                            Exit For
                        End If
                    Next
                End If
                Dim tNode As New TreeNode(xNode.Name)
                Dim bIsTxt As Boolean = False
                If tNode.Text = "#text" Then
                    tNode.Text = xNode.Value
                    bIsTxt = True
                End If
                If imgIndex <> -1 Then
                    tNode.ImageIndex = imgIndex
                End If
                If bIsTxt Then
                    'inTreeNode.Text += " " + tNode.Text '
                    inTreeNode.Text = tNode.Text
                Else
                    inTreeNode.Nodes.Add(tNode)
                    AddNode(xNode, tNode)
                End If
                i += 1
            End While
        End If
    End Sub
0

Great it works. I tried to change this line a few days ago, but only all things behind "+="... If i would changed it in only "=". Maybe then I would found it by myself some days ago ;-)

So, now I got only one thing which doesn´t work...
The doubleclick-event doesn´t start the programs. I think it doesn´t read the path from the xml!?

At the beginning I got :

Public Class MyTreeNode
        Inherits TreeNode
        Public Property Path As String
    End Class

and this is my double-click-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
    End Sub

If I double click on the programs, always the Exception (Messagebox) comes.

Can you tell me, please, what is wrong?

0

Here the control is inheriting from TreeView instead of TreeNode:

Imports System.Xml
Imports System.Reflection
Imports System.IO
Imports System.Text
Imports System.Text.RegularExpressions

Public Class treeViewLoadXml
    Private Sub treeViewLoadXml_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        MyTreeNode1.Load_XML()
    End Sub
End Class

Public Class MyTreeNode
    Inherits TreeView
    Dim imgLst As New ImageList
    Dim vImgPath(-1) As String, iv As Int32 = 0
    Public Sub Load_XML()
        Try
            ' Get Xml's file stream.

            imgLst.ImageSize = New Drawing.Size(30, 27)

            Dim vPath() As String = Split(Application.ExecutablePath, "\")
            vPath(vPath.Length - 1) = "XMLFile2.xml"
            Dim filepath As String = Join(vPath, "\")
            Dim _rstream As New FileStream(filepath, FileMode.Open)

            ' Load Xml document.
            '
            Dim dom As New XmlDocument()
            dom.Load(_rstream)
            _rstream.Close()

            ' Initialize treeView control.
            '
            BeginUpdate()
            Nodes.Clear()
            Nodes.Add(New TreeNode(dom.DocumentElement.Name))

            ' Populate the treeView with the dom nodes.
            '
            AddNode(dom.DocumentElement, Nodes(0))
            If iv Then
                For i = 0 To iv - 1
                    imgLst.Images.Add(Image.FromFile(vImgPath(i)))
                Next
                ImageList = imgLst
            End If
            EndUpdate()
            ExpandAll()
        Catch xmlEx As XmlException
            MessageBox.Show(xmlEx.Message)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Private Sub AddNode(ByVal inXmlNode As XmlNode, ByVal inTreeNode As TreeNode)
        Dim i As Integer
        If inXmlNode.HasChildNodes Then
            Dim nodeList As XmlNodeList
            nodeList = inXmlNode.ChildNodes
            i = 0
            While i <= nodeList.Count - 1
                Dim xNode As XmlNode = inXmlNode.ChildNodes(i)
                Dim imgIndex As Int32 = -1
                If xNode.Attributes IsNot Nothing Then
                    For Each att As XmlAttribute In xNode.Attributes
                        If att.Name = "Icon" Then
                            imgIndex = addImage(att.Value)
                            Exit For
                        End If
                    Next
                End If
                Dim tNode As New TreeNode(xNode.Name)
                Dim bIsTxt As Boolean = False
                If tNode.Text = "#text" Then
                    tNode.Text = xNode.Value
                    bIsTxt = True
                End If
                If imgIndex <> -1 Then
                    tNode.ImageIndex = imgIndex
                End If
                If bIsTxt Then
                    'inTreeNode.Text += " " + tNode.Text '
                    inTreeNode.Text = tNode.Text
                Else
                    inTreeNode.Nodes.Add(tNode)
                    AddNode(xNode, tNode)
                End If
                i += 1
            End While
            If inXmlNode.Attributes IsNot Nothing AndAlso _
            inXmlNode.Attributes("Path") IsNot Nothing Then
                inTreeNode.Name = inXmlNode.Attributes("Path").Value
            End If
        End If
    End Sub
    Function addImage(path As String) As Int32
        Dim i As Int32 = Array.IndexOf(vImgPath, path)
        If i = -1 AndAlso IO.File.Exists(path) Then
            ReDim Preserve vImgPath(iv)
            Me.vImgPath(iv) = path
            i = iv
            iv += 1
        End If
        Return i
    End Function

    Private Sub me_NodeMouseDoubleClick(sender As Object, e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles MyBase.NodeMouseDoubleClick
        Try
            Dim Path As String = SelectedNode.Name
            If File.Exists(Path) Then
                Process.Start(Path)
            End If
        Catch ex As Exception
            MsgBox("kein Programm gefunden")
        End Try
    End Sub
End Class
0

Note that lines #92 to #95 reserve the path in 'Name' property, which is latter used in line #113 to execute the file in Process.Start

0

I can´t start the Sub "Load_XML" by

MyTreeNode1.Load_XML()

Error-code is something like : "Load_XML" is not declared. The object can not be used because the protection level.

I allready changed it in only Load_XML() but it is allways the same problem.

0

That's because your control's name is perhaps treeview1 -a treeview control- and if it's not an instance of MyTreeNode. To replace treeView1 by an instance of MyTreeNode look in the tools bar (in mine's near the top of the tools' bar).

0

I´m sorry, but I doesn´t understand what you mean.

Yes, my TreeView control on my form is called "TreeView1". If I change "MyTreeNode1.Load_XML()" to "TreeView1.Load_XML()" it is still the same problem.
Where can I find it to change the instances? I searched it also in the propertys from the TreeView1-Control, because I didn´t find something like that at the top of my toolsbar.

0

The class starting at lines #13 and #14 define a user control (which inherits from TreeView):

Public Class MyTreeNode
    Inherits TreeView
    ...

You should add this user control to your form, i.e., build the project and add the control from the ToolBox. You can find an explanation Here

1

Ok, forget the last I said and here is the code again, supposed the TreeView control is named TreeView1:

Imports System.Xml
Imports System.IO
Imports System.Text
Imports System.Text.RegularExpressions

Public Class treeViewLoadXml
    Dim imgLst As New ImageList
    Dim vImgPath(-1) As String, iv As Int32 = 0
    Private Sub treeViewLoadXml_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Load_XML()
    End Sub
    Public Sub Load_XML()
        Try
            ' Get Xml's file stream.

            imgLst.ImageSize = New Drawing.Size(30, 27)

            Dim vPath() As String = Split(Application.ExecutablePath, "\")
            vPath(vPath.Length - 1) = "XMLFile2.xml"
            Dim filepath As String = Join(vPath, "\")
            Dim _rstream As New FileStream(filepath, FileMode.Open)

            ' Load Xml document.
            '
            Dim dom As New XmlDocument()
            dom.Load(_rstream)
            _rstream.Close()

            ' Initialize treeView control.
            '
            TreeView1.BeginUpdate()
            TreeView1.Nodes.Clear()
            TreeView1.Nodes.Add(New TreeNode(dom.DocumentElement.Name))

            ' Populate the treeView with the dom nodes.
            '
            AddNode(dom.DocumentElement, TreeView1.Nodes(0))
            If iv Then
                For i = 0 To iv - 1
                    imgLst.Images.Add(Image.FromFile(vImgPath(i)))
                Next
                TreeView1.ImageList = imgLst
            End If
            TreeView1.EndUpdate()
            TreeView1.ExpandAll()
        Catch xmlEx As XmlException
            MessageBox.Show(xmlEx.Message)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Private Sub AddNode(ByVal inXmlNode As XmlNode, ByVal inTreeNode As TreeNode)
        Dim i As Integer
        If inXmlNode.HasChildNodes Then
            Dim nodeList As XmlNodeList
            nodeList = inXmlNode.ChildNodes
            i = 0
            While i <= nodeList.Count - 1
                Dim xNode As XmlNode = inXmlNode.ChildNodes(i)
                Dim imgIndex As Int32 = -1
                If xNode.Attributes IsNot Nothing Then
                    For Each att As XmlAttribute In xNode.Attributes
                        If att.Name = "Icon" Then
                            imgIndex = addImage(att.Value)
                            Exit For
                        End If
                    Next
                End If
                Dim tNode As New TreeNode(xNode.Name)
                Dim bIsTxt As Boolean = False
                If tNode.Text = "#text" Then
                    tNode.Text = xNode.Value
                    bIsTxt = True
                End If
                If imgIndex <> -1 Then
                    tNode.ImageIndex = imgIndex
                End If
                If bIsTxt Then
                    'inTreeNode.Text += " " + tNode.Text '
                    inTreeNode.Text = tNode.Text
                Else
                    inTreeNode.Nodes.Add(tNode)
                    AddNode(xNode, tNode)
                End If
                i += 1
            End While
            If inXmlNode.Attributes IsNot Nothing AndAlso _
            inXmlNode.Attributes("Path") IsNot Nothing Then
                inTreeNode.Name = inXmlNode.Attributes("Path").Value
            End If
        End If
    End Sub
    Function addImage(path As String) As Int32
        Dim i As Int32 = Array.IndexOf(vImgPath, path)
        If i = -1 AndAlso IO.File.Exists(path) Then
            ReDim Preserve vImgPath(iv)
            Me.vImgPath(iv) = path
            i = iv
            iv += 1
        End If
        Return i
    End Function

    Private Sub me_NodeMouseDoubleClick(sender As Object, e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseDoubleClick
        Try
            Dim Path As String = TreeView1.SelectedNode.Name
            If File.Exists(Path) Then
                Process.Start(Path)
            End If
        Catch ex As Exception
            MsgBox("kein Programm gefunden")
        End Try
    End Sub
End Class
0

Of course your class seems to be called XmlTreeView so replace line #7 Public Class treeViewLoadXml by Public Class XmlTreeView. I case of doubt please tell me.

0

Thank you very much.

I changed the DoubleClick-Sub, because I don´t want any messagebox, if I click on a folder... But I think, my solution isn´t the best, because allways I have to change the vb-code, if I create a new folder in my xml-file.

    Private Sub TreeView1_NodeMouseDoubleClick(sender As Object, e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseDoubleClick
        Try
            Dim Path As String = TreeView1.SelectedNode.Name
            If e.Node.Text = "Programme" Then
            ElseIf e.Node.Text = "Feld 1" Then
            ElseIf e.Node.Text = "Home Place" Then
            Else
                If File.Exists(Path) Then
                    Process.Start(Path)
                Else
                    MsgBox("kein Programm gefunden")
                End If
            End If
        Catch ex As Exception
            MsgBox("kein Programm gefunden")
        End Try
    End Sub
1

You may do a trick: set a numberic value to Path attribute for Programme, Felt, and so on:

<?xml version="1.0" encoding="utf-8"?>
<Programme Path="1">
  <Feld Icon="Folder.ico" Path="2">
    Feld 1<VisualStudio Path="C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\devenv.exe" Icon="VS.png">Visual Studio</VisualStudio>
    <Excel Path="C:\Program Files\Microsoft Office 15\root\office15\EXCEL.EXE" Icon="Excel.png">Excel</Excel>
  </Feld>
  <Home Icon="Folder.ico" Path="3">
    Home Place<Notepad Path="C:\Program Files\Notepad++\notepad++.exe" Icon="notepad.png">Notepad ++</Notepad>
  </Home>
</Programme>

Then change mouse double click to examine if Path is numeric.

Private Sub me_NodeMouseDoubleClick(sender As Object, e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseDoubleClick
    Try
        Dim Path As String = TreeView1.SelectedNode.Name
        If IsNumeric(Path) Then
            Dim nAction As Int32 = Int32.Parse(Path)
            Select Case nAction
                Case 1
                    ' do job for Path="1" ' 
                Case 2
                    ' do job for Path="2" '
                Case 3
                    ' do job for Path="3" '
            End Select
        Else
            If File.Exists(Path) Then
                Process.Start(Path)
            End If
        End If
    Catch ex As Exception
        MsgBox("kein Programm gefunden")
    End Try
End Sub

Whenever you add a new folder to the XML you may set 'Path' attribute to the number of job it must do.

0

5 months ago and now i got a new problem in my code ;-)

I got a node in my xml-File for a java-link. it looks like "C:\Program Files (x86)\Tool7\javaw.exe -splash:splash.png -cp ./cl/cl.jar; -Dawt.useSystemAAFontSettings=on"

If i start the tool by double click on the node, it doesn´t start it, because it doesn´t load the arguments.

Next i createt a new attribut (called Argument) in the xml-file at this node and the value in "Argumente" is "-splash:splash.png -cp ./cl/cl.jar; -Dawt.useSystemAAFontSettings=on". But it still doesn´t work.

So, it doesn´t read the attribut in the XML, but i don´t know why. I hope you can help me again? :)

Here is the XML-Code :

<?xml version="1.0" encoding="UTF-8"?>
<Programme Path="1">
  <Feld Icon="Folder.ico" Path="2">Feld 1<VisualStudio Path="C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\devenv.exe" Icon="VS.png">Visual Studio</VisualStudio>
    <Excel Path="C:\Program Files\Microsoft Office 15\root\office15\EXCEL.EXE" Icon="Excel.png">Excel</Excel>
  </Feld>
  <Home Icon="Folder.ico" Path="3">Home Place<Notepad Path="C:\Program Files\Notepad++\notepad++.exe" Icon="notepad.png">Notepad ++</Notepad>
    <JaTool Path="C:\Program Files (x86)\Tool7\javaw.exe" Argumente="-splash:splash.png -cp ./cl/cl.jar; -Dawt.useSystemAAFontSettings=on" Icon="JaTool.ico" RunPath="C:\Program Files (x86)\Tool7\bin\..">JaTool</JaTool>
  </Home>
</Programme>

Here is my VB-Code :

Option Strict On
Imports System.Xml
Imports System.IO
Imports System.Text
Imports System.Text.RegularExpressions
Public Class treeViewLoadXml
    Dim imgLst As New ImageList
    Dim vImgPath(-1) As String, iv As Int32 = 0
    Private Sub treeViewLoadXml_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Load_XML()
    End Sub
    Public Sub Load_XML()
        Try
            ' Get Xml's file stream.
            imgLst.ImageSize = New Drawing.Size(30, 27)
            Dim vPath() As String = Split(Application.ExecutablePath, "\")
            vPath(vPath.Length - 1) = "XMLFile1.xml"
            Dim filepath As String = Join(vPath, "\")
            Dim _rstream As New FileStream(filepath, FileMode.Open)
            ' Load Xml document.
            '
            Dim dom As New XmlDocument()
            dom.Load(_rstream)
            _rstream.Close()
            ' Initialize treeView control.
            '
            TreeView1.BeginUpdate()
            TreeView1.Nodes.Clear()
            TreeView1.Nodes.Add(New TreeNode(dom.DocumentElement.Name))
            ' Populate the treeView with the dom nodes.
            '
            AddNode(dom.DocumentElement, TreeView1.Nodes(0))
            If CBool(iv) Then
                For i = 0 To iv - 1
                    imgLst.Images.Add(Image.FromFile(vImgPath(i)))
                Next
                TreeView1.ImageList = imgLst
            End If
            TreeView1.EndUpdate()
            'TreeView1.ExpandAll()
        Catch xmlEx As XmlException
            MessageBox.Show(xmlEx.Message)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Private Sub AddNode(ByVal inXmlNode As XmlNode, ByVal inTreeNode As TreeNode)
        Dim i As Integer
        If inXmlNode.HasChildNodes Then
            Dim nodeList As XmlNodeList
            nodeList = inXmlNode.ChildNodes
            i = 0
            While i <= nodeList.Count - 1
                Dim xNode As XmlNode = inXmlNode.ChildNodes(i)
                Dim imgIndex As Int32 = -1
                Dim SelimgIndex As Int32 = -1
                If xNode.Attributes IsNot Nothing Then
                    For Each att As XmlAttribute In xNode.Attributes
                        If att.Name = "Icon" Then
                            imgIndex = addImage(att.Value)
                            SelimgIndex = addImage(att.Value)
                            Exit For
                        End If
                    Next
                End If
                Dim tNode As New TreeNode(xNode.Name)
                Dim bIsTxt As Boolean = False
                If tNode.Text = "#text" Then
                    tNode.Text = xNode.Value
                    bIsTxt = True
                End If
                If imgIndex <> -1 Then
                    tNode.ImageIndex = imgIndex
                End If
                If SelimgIndex <> -1 Then
                    tNode.SelectedImageIndex = SelimgIndex
                End If
                If bIsTxt Then
                    'inTreeNode.Text += " " + tNode.Text '
                    inTreeNode.Text = tNode.Text
                Else
                    inTreeNode.Nodes.Add(tNode)
                    AddNode(xNode, tNode)
                End If
                i += 1
            End While
            If inXmlNode.Attributes IsNot Nothing AndAlso inXmlNode.Attributes("Path") IsNot Nothing Then
                inTreeNode.Name = inXmlNode.Attributes("Path").Value
            End If
            If inXmlNode.Attributes IsNot Nothing AndAlso inXmlNode.Attributes("Argumente") IsNot Nothing Then
                inTreeNode.Name = inXmlNode.Attributes("Argumente").Value
            End If
            If inXmlNode.Attributes IsNot Nothing AndAlso inXmlNode.Attributes("RunPath") IsNot Nothing Then
                inTreeNode.Name = inXmlNode.Attributes("RunPath").Value
            End If
        End If
    End Sub
    Function addImage(path As String) As Int32
        Dim i As Int32 = Array.IndexOf(vImgPath, path)
        If i = -1 AndAlso IO.File.Exists(path) Then
            ReDim Preserve vImgPath(iv)
            Me.vImgPath(iv) = path
            i = iv
            iv += 1
        End If
        Return i
    End Function
    Private Sub TreeView1_NodeMouseDoubleClick(sender As Object, e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseDoubleClick
        Try
            Dim Path As String = TreeView1.SelectedNode.Name
            Dim Argumente As String = TreeView1.SelectedNode.Name
            Dim RunPath As String = TreeView1.SelectedNode.Name

            If IsNumeric(Path) Then
                Dim nAction As Int32 = Int32.Parse(Path)
                Select Case nAction
                    Case 1 'wenn was gemacht werden soll, wenn Path ist nur eine Zahl
                        ' do job for Path="1" ' 
                    Case 2
                        ' do job for Path="2" '
                    Case 3
                        ' do job for Path="3" '
                End Select
            Else
                Select Case e.Node.Text
                    Case "Visual Studio"
                        MsgBox("Visual Studio")
                        '...
                    Case "Excel"
                        MsgBox("Excel")
                        '...
                    Case "Notepad ++"
                        MsgBox("Notepad")
                        '...
                End Select
                If File.Exists(Path) Then
                    Process.Start(Path)
                    'Dim MyProcess As New Process()
                    'MyProcess.StartInfo.FileName = Path
                    'MyProcess.StartInfo.Arguments = Argumente '(optional)
                    'MyProcess.Start()
                End If
            End If
        Catch ex As Exception
            MsgBox("kein Programm gefunden")
        End Try
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim Path As String = TreeView1.SelectedNode.Name
        Dim Argumente As String = TreeView1.SelectedNode.Name
        Dim RunPath As String = TreeView1.SelectedNode.Name
        Label1.Text = Path
        Label2.Text = Argumente
        Label3.Text = RunPath
    End Sub
End Class
1

Think that TreeNode can't hold custom arguments. Change the xml so the path is complete:

<?xml version="1.0" encoding="UTF-8"?>

<Programme Path="1">
  <Feld Icon="Folder.ico" Path="2">Feld 1<VisualStudio Path="C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\devenv.exe" Icon="VS.png">Visual Studio</VisualStudio>

    <Excel Path="C:\Program Files\Microsoft Office 15\root\office15\EXCEL.EXE" Icon="Excel.png">Excel</Excel>
  </Feld>
     If File.Exists(Path) Then
  <Home Icon="Folder.ico" Path="3">Home Place<Notepad Path="C:\Program Files\Notepad++\notepad++.exe" Icon="notepad.png">Notepad ++</Notepad>
    <JaTool Path="C:\Program Files (x86)\Tool7\javaw.exe -splash:splash.png -cp ./cl/cl.jar; -Dawt.useSystemAAFontSettings=on" Icon="JaTool.ico" RunPath="C:\Program Files (x86)\Tool7\bin\..">JaTool</JaTool>

  </Home>
</Programme>

and then change the following:

     If File.Exists(Path) Then
        Process.Start(Path)
       'Dim MyProcess As New Process()
       'MyProcess.StartInfo.FileName = Path
       'MyProcess.StartInfo.Arguments = Argumente '(optional)
       Dim pos As Int32 = InStr(Path, " -")
      'MyProcess.Start()
    End If

by:

      Dim pos As Int32 = InStr(Path, " -")
      Dim Argumente As String = ""
      If pos Then
          Argumente = Mid(Path, pos)
          Path = Mid(Path, 1, pos - 1)
      End If
      If File.Exists(Path) Then
          Process.Start(Path, Argumente)
      End If
1

By custom arguments I mean custom attributes. In a xml file a node may have as many attributes as you wish, but Treenode control has it's own and fixed properties. In case you need to add extra attributes to Treenode you may create a custom class derived from Treenode.

1

The easiest way to store the attributes is in a new class, in order to extend the NodeTree class. So when reading the xml attributes, store them in this -let's call it- 'extendNode' class. Then, in the TreeView node all it's needed is to set the node's 'Tag' property to the 'extendNode' instance where the attributes are.

Imports System.Xml
Imports System.IO
Imports System.Text

Public Class treeViewLoadXml
    Dim imgLst As New ImageList
    Dim vImgPath(-1) As String, iv As Int32 = 0
    Private Sub treeViewLoadXml_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Load_XML()
    End Sub
    Public Sub Load_XML()
        Try
            ' Get Xml's file stream.

            imgLst.ImageSize = New Drawing.Size(30, 27)

            Dim vPath() As String = Split(Application.ExecutablePath, "\")
            vPath(vPath.Length - 1) = "XMLFile2.xml"
            Dim filepath As String = Join(vPath, "\")
            Dim _rstream As New FileStream(filepath, FileMode.Open)

            ' Load Xml document.
            '
            Dim dom As New XmlDocument()
            dom.Load(_rstream)
            _rstream.Close()

            ' Initialize treeView control.
            '
            TreeView1.BeginUpdate()
            TreeView1.Nodes.Clear()
            TreeView1.Nodes.Add(New TreeNode(dom.DocumentElement.Name))

            ' Populate the treeView with the dom nodes.
            '
            AddNode(dom.DocumentElement, TreeView1.Nodes(0))
            If iv Then
                For i = 0 To iv - 1
                    imgLst.Images.Add(Image.FromFile(vImgPath(i)))
                Next
                TreeView1.ImageList = imgLst
            End If
            TreeView1.EndUpdate()
            TreeView1.ExpandAll()
        Catch xmlEx As XmlException
            MessageBox.Show(xmlEx.Message)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Private Sub AddNode(ByVal inXmlNode As XmlNode, ByVal inTreeNode As TreeNode)
        Dim i As Integer
        If inXmlNode.HasChildNodes Then
            Dim nodeList As XmlNodeList
            nodeList = inXmlNode.ChildNodes
            i = 0
            While i <= nodeList.Count - 1
                Dim xNode As XmlNode = inXmlNode.ChildNodes(i)
                Dim imgIndex As Int32 = -1

                Dim eN As New extendNode ' eN will hold the attributes we wish'

                If xNode.Attributes IsNot Nothing Then
                    For Each att As XmlAttribute In xNode.Attributes
                        If att.Name = "Icon" Then
                            imgIndex = addImage(att.Value)
                            Exit For
                        Else
                            ' store the remaining attributes: '
                            eN.attr.Add(att.Name, att.Value)
                        End If
                    Next
                End If
                Dim tNode As New TreeNode(xNode.Name)
                Dim bIsTxt As Boolean = False
                If tNode.Text = "#text" Then
                    tNode.Text = xNode.Value
                    bIsTxt = True
                End If
                tNode.Tag = eN
                If imgIndex <> -1 Then
                    tNode.ImageIndex = imgIndex
                End If
                If bIsTxt Then
                    'inTreeNode.Text += " " + tNode.Text '
                    inTreeNode.Text = tNode.Text
                Else
                    inTreeNode.Nodes.Add(tNode)
                    AddNode(xNode, tNode)
                End If
                i += 1
            End While
            If inXmlNode.Attributes IsNot Nothing AndAlso
            inXmlNode.Attributes("Path") IsNot Nothing Then
                inTreeNode.Name = inXmlNode.Attributes("Path").Value
            End If
        End If
    End Sub
    Function addImage(path As String) As Int32
        Dim i As Int32 = Array.IndexOf(vImgPath, path)
        If i = -1 AndAlso IO.File.Exists(path) Then
            ReDim Preserve vImgPath(iv)
            Me.vImgPath(iv) = path
            i = iv
            iv += 1
        End If
        Return i
    End Function

    Private Sub me_NodeMouseDoubleClick(sender As Object, e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseDoubleClick
        Try
            Dim Path As String = TreeView1.SelectedNode.Name
            If IsNumeric(Path) Then
                Dim nAction As Int32 = Int32.Parse(Path)
                Select Case nAction
                    Case 1
                        ' do job for Path="1" ' 
                    Case 2
                        ' do job for Path="2" '
                    Case 3
                        ' do job for Path="3" '
                End Select
            Else
                Dim arg
                Try
                    ' Recover the "Argumente" attribute value: '
                    arg = TreeView1.SelectedNode.Tag.attr("Argumente")
                Catch
                End Try
                If File.Exists(Path) Then
                    Process.Start(Path, arg)
                End If
            End If
        Catch ex As Exception
            MsgBox("kein Programm gefunden")
        End Try
    End Sub
End Class
Class extendNode
    ' a dictionary to store the attribute name, value pair: '
    Public attr As New Dictionary(Of String, String)
End Class

Edited by xrj

1

The xml file should look like the following:

<?xml version="1.0" encoding="UTF-8"?>
<Programme Path="1">
  <Feld Icon="Folder.ico" Path="2">Feld 1<VisualStudio Path="C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\devenv.exe" Icon="VS.png">Visual Studio</VisualStudio>
    <Excel Path="C:\Program Files\Microsoft Office 15\root\office15\EXCEL.EXE" Icon="Excel.png">Excel</Excel>
  </Feld>
  <Home Icon="Folder.ico" Path="3">Home Place<Notepad Path="C:\Program Files\Notepad++\notepad++.exe" Icon="notepad.png">Notepad ++</Notepad>
    <JaTool Path="C:\Program Files (x86)\Tool7\javaw.exe" Argumente="-splash:splash.png -cp ./cl/cl.jar; -Dawt.useSystemAAFontSettings=on" Icon="JaTool.ico" RunPath="C:\Program Files (x86)\Tool7\bin\..">JaTool</JaTool>
  </Home>
</Programme>
0

Thank you very much. I will try it and make some tests.

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.