Sauvegarder un Treeview puis ouvrir la sauvegarde

Contenu du snippet

Bonjour bonsoir a tous je vous présente un code capable d'enregistrer une modification ou simplement un TreeView puis de le réouvrir.
Voici le code pour enregistrer :

    Private Sub SaveTree()
        Dim buffer As New System.Text.StringBuilder
        For Each rootNode As TreeNode In TreeView1.Nodes
            BuildTreeString(rootNode, buffer)
        Next
        IO.File.WriteAllText(path, buffer.ToString)
    End Sub

    Private Sub BuildTreeString(ByVal rootNode As TreeNode, ByRef buffer As System.Text.StringBuilder)
        buffer.Append("~" & rootNode.Text)
        buffer.Append(Environment.NewLine)
        buffer.Append("?" & rootNode.Tag)
        buffer.Append(Environment.NewLine)
        buffer.Append("#" & rootNode.ForeColor.toargb)
        buffer.Append(Environment.NewLine)
        For Each childNode As TreeNode In rootNode.Nodes
            buffer.Append("/")
            buffer.Append(Environment.NewLine)
            buffer.Append("/&" & childNode.Text)
            buffer.Append(Environment.NewLine)
            buffer.Append("/°" & childNode.Tag)
            buffer.Append(Environment.NewLine)
            buffer.Append("/|" & childNode.ForeColor.toargb)
            buffer.Append(Environment.NewLine)
        Next
        buffer.Append("_")
        buffer.Append(Environment.NewLine)
    End Sub


et voici le code pour ouvrir le fichier de sauvegarde :

Private Sub LoadTree()
        '/initialisation
        Dim nd As New TreeNode
        nd.Text = ""
        nd.Tag = "00.00"
        nd.ForeColor = Color.Black

        Dim nod As String = ""

        Dim ch As New TreeNode

        Dim lines As List(Of String) = System.IO.File.ReadAllLines(path).ToList
        For i As Integer = 0 To lines.Count - 1

            'node principale

            If lines(i).Contains("~") Then
                nd.Text = lines(i).Replace("~", "")
                nod = lines(i).Replace("~", "")
                    ElseIf lines(i).Contains("?") Then
                nd.Tag = lines(i).Replace("?", "")
            ElseIf lines(i).Contains("#") Then
                nd.ForeColor = Color.Fromargb(lines(i).Replace("#", ""))

                'enfants
            ElseIf lines(i).Contains("/") Then

                If lines(i).Contains("/&") Then
                    nd.Nodes.Add(lines(i).Replace("/&", ""))
                        ElseIf lines(i).Contains("/°") Then
                    nd.LastNode.Tag = lines(i).Replace("/°", "")
                ElseIf lines(i).Contains("/|") Then
                    nd.LastNode.ForeColor = Color.Fromargb(lines(i).Replace("/|", ""))
                End If

                'construction de nd
            ElseIf lines(i).Contains("_") Then
                'réinitialisation
                TreeView1.Nodes.Add(nd.Clone())
                nd.Remove()
                nd.Nodes.Clear()
            End If
        Next
    End Sub


voilà pour ce tout petit tutoriel et désoler si je me suis tromper d'endroit je suis nouveau :)

Compatibilité : 1.0

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.