[VB.Net] Build d'explorer TreeView

    Publicités

Users Who Are Viewing This Thread (Total: 0, Members: 0, Guests: 0)

Status
Not open for further replies.
Apr 26, 2011
1,315
2
944
26
In my home
Discord
brokeos
Bonjour à tous, voici mon problème :

J'aimerais afficher ceci dans mon TreeView :

img


Par rapport à ce fichier:

Code:
# type|Location|name|ID
folder||Settings|Folder_Settings
folder||MyProject|Folder_MyProject
folder|MyProject|My|Folder_My
folder|MyProject/My|Help|Folder_Help
folder|Settings|App|Folder_App
file|MyProject/My|test.vb|File_Test

Alors j'ai pondu ce code:

Code:
Dim lines As String() = _files.Split(vbLf) 'c'est le fichier text de en haut
            Dim Folders As New List(Of String)
            Dim FoldersWithParents As New List(Of String)
            Dim MaxCount As Integer = 0
            Dim Files As New List(Of String)
            For Each line As String In lines
                If line.Split("|").First = "folder" Then
                    Folders.Add(line)
                Else
                    Files.Add(line)
                End If
            Next
            For Each Folder As String In Folders
                Dim data() As String = Folder.Split("|")
                If Not data(1) = "" Then
                    Dim Parents() As String = data(1).Split("/")
                    If Parents.Count > MaxCount Then
                        MaxCount = Parents.Count
                    End If
                End If
            Next
            For invoked_i As Integer = 0 To MaxCount
                For i As Integer = 0 To Folders.Count - 1
                    Dim data() As String = Folders.Item(i).Split("|")
                    Dim folder_parents() As String = (data(1) & "/").Split("/")
                    If folder_parents(invoked_i) = "" Then
                        Dim NewNode As New TreeNode
                        NewNode.ImageIndex = 0
                        NewNode.SelectedImageIndex = 0
                        NewNode.Text = data(2)
                        NewNode.Tag = data(3)
                        NewNode.ToolTipText = "folder:" & data(3)
                        TreeView.Invoke(Sub()
                                            If Not folder_parents(0) = "" Then
                                                If TreeView.GetNodeByFullPath(data(1)) IsNot Nothing Then
                                                    TreeView.GetNodeByFullPath(data(1)).Nodes.Add(NewNode)
                                                Else
                                                    TreeView.Nodes.Add(NewNode)
                                                End If
                                            Else
                                                TreeView.Nodes.Add(NewNode)
                                            End If
                                        End Sub)
                    Else
                        FoldersWithParents.Add(Folders.Item(i))
                    End If
                Next
                Folders = FoldersWithParents
            Next

Code:
<Extension()> _
        Public Function GetNodeByFullPath(ByVal TreeView As TreeView, ByVal FullPath As String) As TreeNode
            Dim TreeNode() As TreeNode = TreeView.Nodes.Find(FullPath, True)
            If TreeNode.Count > 0 Then
                Return TreeNode(0)
            Else
                Return Nothing
            End If
        End Function

Seulement ça ne fonctionne pas ! :(
 

ragnarock

Membre actif
Mar 22, 2010
194
0
917
Salut,

Je regarde pour te pondre un truc qui marche.

Mais j'ai l'impression que tu te compliques la vie avec le fichier
un truc dans ce genre suffit :
Code:
folder|le
folder|le/chemin
folder|le/chemin/asdasdas
file|le/chemin/asdasdas/ficher.vb

Désolé, mais j'ai la flemme de reprendre ton code pour voir ce qui ne va pas, tu peux reprendre celui que j'ai fait ou t'en inspirer pour "réparer" le tien.

et voilà :

[C#, l'original]
Code:
            var folders = new List<string>();
            var files = new List<string>();
            var content = new List<string>
                          {
                              @"folder||Settings|Folder_Settings",
                              @"folder||MyProject|Folder_MyProject",
                              @"folder|MyProject|My|Folder_My",
                              @"folder|MyProject/My|Help|Folder_Help",
                              @"folder|Settings|App|Folder_App",
                              @"file|MyProject/My|test.vb|File_Test",

                          };

            foreach (var line in content)
                if (line.StartsWith("folder"))
                    folders.Add(line.Remove(0, 7));
                else if (line.StartsWith("file"))
                    files.Add(line.Remove(0, 5));

            foreach (var folder in folders)
            {
                var data = folder.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
                if (folder.StartsWith("|"))
                {
                    // top-level node
                    var newNode = new TreeNode(data[0]) { Name = data[0] };
                    treeView1.Nodes.Add(newNode);
                }
                else
                {
                    var path = data[0].Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
                    TreeNodeCollection coll = treeView1.Nodes;
                    foreach (var pathSegment in path)
                    {
                        if (coll.ContainsKey(pathSegment))
                        {
                            var node = coll.Cast<TreeNode>().First(tn => tn.Name == pathSegment);
                            coll = node.Nodes;
                        }
                        else
                        {
                            var newNode = new TreeNode(pathSegment) { Name = pathSegment };
                            coll.Add(newNode);
                            coll = newNode.Nodes;
                        }
                    }

                    coll.Add(new TreeNode(data[1]) { Name = data[1] }); //folder name
                }
            }

            foreach (var file in files)
            {
                var data = file.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);

                if (file.StartsWith("|"))
                {
                    // top-level node
                    var newNode = new TreeNode(data[0]) { Name = data[0] };
                    treeView1.Nodes.Add(newNode);
                }
                else
                {
                    var path = data[0].Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
                    TreeNodeCollection coll = treeView1.Nodes;
                    foreach (var pathSegment in path)
                    {
                        if (coll.ContainsKey(pathSegment))
                        {
                            var node = coll.Cast<TreeNode>().First(tn => tn.Name == pathSegment);
                            coll = node.Nodes;
                        }
                        else
                        {
                            var newNode = new TreeNode(pathSegment) { Name = pathSegment };
                            coll.Add(newNode);
                            coll = newNode.Nodes;
                        }
                    }

                    coll.Add(new TreeNode(data[1]) { Name = data[1] }); //file name
                }
            }

[Vb.Net]
Code:
Dim folders = New List(Of String)()
Dim files = New List(Of String)()

Dim content = New List(Of String)() From { _
	"folder||Settings|Folder_Settings", _
	"folder||MyProject|Folder_MyProject", _
	"folder|MyProject|My|Folder_My", _
	"folder|MyProject/My|Help|Folder_Help", _
	"folder|Settings|App|Folder_App", _
	"file|MyProject/My|test.vb|File_Test" _
}

For Each line As var In content
	If line.StartsWith("folder") Then
		folders.Add(line.Remove(0, 7))
	ElseIf line.StartsWith("file") Then
		files.Add(line.Remove(0, 5))
	End If
Next

For Each folder As var In folders
	Dim data = folder.Split(New () {"|"C}, StringSplitOptions.RemoveEmptyEntries)
	If folder.StartsWith("|") Then
		' top-level node
		Dim newNode = New TreeNode(data(0)) With { _
			Key .Name = data(0) _
		}
		treeView1.Nodes.Add(newNode)
	Else
		Dim path = data(0).Split(New () {"/"C}, StringSplitOptions.RemoveEmptyEntries)
		Dim coll As TreeNodeCollection = treeView1.Nodes
		For Each pathSegment As var In path
			If coll.ContainsKey(pathSegment) Then
				Dim node = coll.Cast(Of TreeNode)().First(Function(tn) tn.Name = pathSegment)
				coll = node.Nodes
			Else
				Dim newNode = New TreeNode(pathSegment) With { _
					Key .Name = pathSegment _
				}
				coll.Add(newNode)
				coll = newNode.Nodes
			End If
		Next

			'folder name
		coll.Add(New TreeNode(data(1)) With { _
			Key .Name = data(1) _
		})
	End If
Next

For Each file As var In files
	Dim data = file.Split(New () {"|"C}, StringSplitOptions.RemoveEmptyEntries)

	If file.StartsWith("|") Then
		' top-level node
		Dim newNode = New TreeNode(data(0)) With { _
			Key .Name = data(0) _
		}
		treeView1.Nodes.Add(newNode)
	Else
		Dim path = data(0).Split(New () {"/"C}, StringSplitOptions.RemoveEmptyEntries)
		Dim coll As TreeNodeCollection = treeView1.Nodes
		For Each pathSegment As var In path
			If coll.ContainsKey(pathSegment) Then
				Dim node = coll.Cast(Of TreeNode)().First(Function(tn) tn.Name = pathSegment)
				coll = node.Nodes
			Else
				Dim newNode = New TreeNode(pathSegment) With { _
					Key .Name = pathSegment _
				}
				coll.Add(newNode)
				coll = newNode.Nodes
			End If
		Next

			'file name
		coll.Add(New TreeNode(data(1)) With { _
			Key .Name = data(1) _
		})
	End If
Next

la seule partie importante c'est le parcours du treeview avec la collection pour créer le chemin, le reste c'est basique.

Et tel-quel, ce code ne marche pas si 2 nodes enfant de la même node possèdent le même nom, mais ça devrait pas arriver dans ce cas vu que les nodes représentent des fichier/dossier et que le nom est unique
 
Last edited:
Apr 26, 2011
1,315
2
944
26
In my home
Discord
brokeos
Merci, seulement j'ai un petit problème ! x)
D'abord j'ai du corriger le code (car les traducteurs, merci)

Code:
            Dim folders = New List(Of String)()
            Dim files = New List(Of String)()

            Dim content = New List(Of String)() From { _
                "folder||Settings|Folder_Settings", _
                "folder||MyProject|Folder_MyProject", _
                "folder|MyProject|My|Folder_My", _
                "folder|MyProject/My|Help|Folder_Help", _
                "folder|Settings|App|Folder_App", _
                "file|MyProject/My|test.vb|File_Test"
            }

            For Each line As String In content
                If line.StartsWith("folder") Then
                    folders.Add(line.Remove(0, 7))
                ElseIf line.StartsWith("file") Then
                    files.Add(line.Remove(0, 5))
                End If
            Next

            For Each folder As String In folders
                Dim data = folder.Split("|")
                If folder.StartsWith("|") Then
                    ' top-level node
                    Dim NewNode As New TreeNode
                    NewNode.Name = data(0)
                    NewNode.ImageIndex = 0
                    NewNode.SelectedImageIndex = 0
                    NewNode.Tag = data.Last
                    NewNode.Text = data(1)
                    NewNode.ToolTipText = "folder:" & data.Last
                    TreeView.Nodes.Add(NewNode)
                Else
                    Dim path = data(0).Split("/")
                    Dim coll As TreeNodeCollection = TreeView.Nodes
                    For Each pathSegment As string In path
                        If coll.ContainsKey(pathSegment) Then
                            Dim node = coll.Cast(Of TreeNode)().First(Function(tn) tn.Name = pathSegment)
                            coll = node.Nodes
                        Else
                            Dim NewNode2 As New TreeNode
                            NewNode2.Name = pathSegment
                            NewNode2.ImageIndex = 0
                            NewNode2.SelectedImageIndex = 0
                            NewNode2.Tag = pathSegment
                            NewNode2.Text = pathSegment
                            NewNode2.ToolTipText = "folder:" & pathSegment
                            coll.Add(NewNode2)
                            coll = NewNode2.Nodes
                        End If
                    Next

                    'folder name
                    Dim NewNode As New TreeNode
                    NewNode.Name = data(1)
                    NewNode.ImageIndex = 0
                    NewNode.SelectedImageIndex = 0
                    NewNode.Tag = data.Last
                    NewNode.Text = data(1)
                    NewNode.ToolTipText = "folder:" & data.Last
                    coll.Add(NewNode)
                End If
            Next

            For Each file As String In files
                Dim data = file.Split("|")

                If file.StartsWith("|") Then
                    ' top-level node
                    Dim NewNode As New TreeNode
                    NewNode.Name = data(0)
                    NewNode.ImageIndex = 1
                    NewNode.SelectedImageIndex = 1
                    NewNode.Tag = data.Last
                    NewNode.Text = data(1)
                    NewNode.ToolTipText = "folder2:" & data.Last
                    TreeView.Nodes.Add(NewNode)
                Else
                    Dim path = data(0).Split("/")
                    Dim coll As TreeNodeCollection = TreeView.Nodes
                    For Each pathSegment As String In path
                        If coll.ContainsKey(pathSegment) Then
                            Dim node = coll.Cast(Of TreeNode)().First(Function(tn) tn.Name = pathSegment)
                            coll = node.Nodes
                        Else
                            Dim NewNode2 As New TreeNode
                            NewNode2.Name = pathSegment
                            NewNode2.ImageIndex = 1
                            NewNode2.SelectedImageIndex = 1
                            NewNode2.Tag = pathSegment
                            NewNode2.Text = pathSegment
                            NewNode2.ToolTipText = "folder2:" & pathSegment
                            coll.Add(NewNode2)
                            coll = NewNode2.Nodes
                        End If
                    Next

                    'file name
                    Dim NewNode As New TreeNode
                    NewNode.Name = data(1)
                    NewNode.ImageIndex = 1
                    NewNode.SelectedImageIndex = 1
                    NewNode.Tag = data.Last
                    NewNode.Text = data(1)
                    NewNode.ToolTipText = "file:" & data.Last
                    coll.Add(NewNode)
                End If
            Next

Et voici ce que j'ai à l'affichage :

img


EDIT :

Résolu, je m'étais juste trompé dans le .Name des dossiers, c'est pas la data 0 mais la 1 ! :)

Merci beaucoup ! :)

EDIT2:

En effet le problème est que quand j'ai un fichier et un dossier qui ont le même nom, bah j'ai ça :

img


Et ma "liste" :

Code:
file||Test|File_Test
folder||Test|Folder_Test
folder|Test|Yolo|Folder_Yolo

Seulement je ne sais pas comment faire en sorte que ça liste par le .tag (qui est unique pour chaque fichier/dossier, donc un dossier ne peut pas avoir le même id que un autre dossier et ne peu pas avoir le même que un fichier)
 
Last edited:

ragnarock

Membre actif
Mar 22, 2010
194
0
917
Tu as pas copié le "StringSplitOptions.RemoveEmptyEntries" des 2 Split, ce qui a donné un petit cafouillage ici :
NewNode.Name = data(0)
vu que le Name est utilisé par cette fonction : coll.ContainsKey(pathSegment)

si ton tag est unique, tu peux le mettre dans le newNode.Name comme ça le ContainsKey devrait marcher avec ton tag.

Si je parle trop chinois dis moi, je te monterai du code.
 
Apr 26, 2011
1,315
2
944
26
In my home
Discord
brokeos
Non c'est bon j'ai compris :)
Je verrais vendredi (quand je rentrerais quoi).
J'ai aussi eu un bug qui faisait que quand un dossier est vide et que j'en créer un dedans, bah le parent ce duplique, j'espère que ça fixeras le problème! :)
 
Apr 26, 2011
1,315
2
944
26
In my home
Discord
brokeos
Voilà j'ai modifier comme tu m'as dit, seulement j'ai toujours le même problème ! :/

Mon code :

Code:
            Dim content As New List(Of String)
            Dim folders As New List(Of String)
            Dim files As New List(Of String)
            For Each line As String In _files.Split(vbLf)
                content.Add(line)
            Next
            For Each line As String In content
                If line.StartsWith("folder") Then
                    folders.Add(line.Remove(0, 7))
                ElseIf line.StartsWith("file") Then
                    files.Add(line.Remove(0, 5))
                End If
            Next
            For Each folder As String In folders
                Dim data = folder.Split({"|"}, StringSplitOptions.RemoveEmptyEntries)
                If (folder.StartsWith("|")) Then
                    Dim newNode = New TreeNode(data(0)) With {.Name = data(0)}
                    TreeView.Invoke(Sub()
                                        TreeView.Nodes.Add(newNode)
                                    End Sub)
                Else
                    Dim path = data(0).Split({"/"}, StringSplitOptions.RemoveEmptyEntries)
                    Dim coll As TreeNodeCollection = TreeView.Nodes
                    For Each pathSegment In path
                        If (coll.ContainsKey(pathSegment)) Then
                            Dim node = coll.Cast(Of TreeNode)().First(Function(tn) tn.Name = pathSegment)
                            coll = node.Nodes
                        Else
                            Dim newNode = New TreeNode(pathSegment) With {.Name = pathSegment}
                            coll.Add(newNode)
                            coll = newNode.Nodes
                        End If
                    Next
                    coll.Add(New TreeNode(data(1)) With {.Name = data(1)})
                End If
            Next
            For Each file As String In files
                Dim data = file.Split({"|"}, StringSplitOptions.RemoveEmptyEntries)
                If (file.StartsWith("|")) Then
                    Dim newNode = New TreeNode(data(0)) With {.Name = data(0)}
                    TreeView.Nodes.Add(newNode)
                Else
                    Dim path = data(0).Split({"/"}, StringSplitOptions.RemoveEmptyEntries)
                    Dim coll As TreeNodeCollection = TreeView.Nodes
                    For Each pathSegment In path
                        If (coll.ContainsKey(pathSegment)) Then
                            Dim node = coll.Cast(Of TreeNode)().First(Function(tn) tn.Name = pathSegment)
                            coll = node.Nodes
                        Else
                            Dim newNode = New TreeNode(pathSegment) With {.Name = pathSegment}
                            coll.Add(newNode)
                            coll = newNode.Nodes
                        End If
                    Next
                    coll.Add(New TreeNode(data(1)) With {.Name = data(1)})
                End If
            Next
 
Last edited:
Status
Not open for further replies.