Bonjour,

je recherche une fonction de recherche dans un treeview un peu spécifique, je vous explique.

L'objectif est que l'on a une textbox, un bouton search et la possibilité de rentrer une chaine de caractère, puis sur le clique du bouton le logiciel recherche dans le treeview.

Nous avons par exemple un treeview comme ceci :
1. Dev
1.1 Deve
1.1.1 Devel
1.1.2 Bonjour
1.1.3 Perso
1.2 Verif
1.2.1 Lapin
1.2.2 Derien
1.2.3 Merci
2. Coucou
2.1 Dons
2.1.1 Develop
2.1.2 CSharp
2.1.3 C++
2.2 Hello
2.2.1 World
2.2.2 Foo
2.2.3 Develap

Dans la textbox, nous entrons "devel".
L'objectif est que le treeview affiche :
1. Dev
1.1 Deve
1.1.1 Devel
2. Coucou
2.1 Dons
2.1.1 Develop
2.2 Hello
2.2.3 Develap

L'objectif est donc, vous l'avez compris, de faire une recherche sur le dernier enfant du treeview et qu'il affiche donc tous les pères.

Pour le moment, voici où j'en suis :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
private List<TreeNode> CurrentNodeMatches = new List<TreeNode>();
        private int LastNodeIndex = 0;
        private string LastSearchText;
 
        private void fbtn_SearchParam_Click(object sender, EventArgs e)
        {
            string searchText = this.ftbx_SearchParam.Text;
 
            if (String.IsNullOrEmpty(searchText))
                return;
 
            if (LastSearchText != searchText)
            {
                CurrentNodeMatches.Clear();
                LastSearchText = searchText;
                LastNodeIndex = 0;
                SearchNode(searchText, ftrv_mainTV.Nodes[0]);
            }
 
            if (LastNodeIndex >= 0 && CurrentNodeMatches.Count > 0 && LastNodeIndex < CurrentNodeMatches.Count)
            {
                TreeNode selectedNode = CurrentNodeMatches[LastNodeIndex];
                TreeNode[] treeNode = new TreeNode[CurrentNodeMatches.Count];
                int i = 0;
 
                foreach (TreeNode tr in CurrentNodeMatches)
                {
                    treeNode[i] = tr;
                    i++;
                }
                LastNodeIndex++;
                this.ftrv_mainTV.Nodes.Clear();
                this.ftrv_mainTV.Nodes.AddRange(treeNode);
            }
        }
 
        private void SearchNode(string SearchText, TreeNode StartNode)
        {
            while (StartNode != null)
            {
                if (StartNode.Text.ToLower().Contains(SearchText.ToLower()))
                {
                    CurrentNodeMatches.Add(StartNode.Parent.Parent);
                    CurrentNodeMatches.Add(StartNode.Parent);
                    CurrentNodeMatches.Add(StartNode);
                }
 
                //Recursive Search
                if (StartNode.Nodes.Count != 0)
                    SearchNode(SearchText, StartNode.Nodes[0]);
                StartNode = StartNode.NextNode;
            }
        }
Merci d'avance pour votre aide !

Amicalement,
VincheZ