Bonjour à tous,
Je rencontre un curieux problème dans l'événement Treeview.DrawNode.
J'ai recopié et modifié ce code qui fonctionne parfaitement :
Mais je souhaiterais ajouter manuellement le Treeview et y ajouter le code afin d'obtenir le même rendu :
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101 Imports System Imports System.Drawing Imports System.Windows.Forms Public Class TreeViewOwnerDraw Inherits Form Private WithEvents myTreeView As TreeView ' Create a Font object for the node tags. Private NodeFont As New Font("Arial", 9.0!) Public Sub New() ' Create and initialize the TreeView control. myTreeView = New TreeView() myTreeView.Dock = DockStyle.Fill myTreeView.ShowLines = False ' Add nodes to the TreeView control. Dim node As TreeNode Dim x As Integer For x = 1 To 3 ' Add a root node to the TreeView control. node = myTreeView.Nodes.Add(String.Format("Task {0}", x)) Dim y As Integer For y = 1 To 3 ' Add a child node to the root node. node.Nodes.Add(String.Format("Subtask {0}", y)) Next y Next x myTreeView.DrawMode = TreeViewDrawMode.OwnerDrawText AddHandler myTreeView.MouseDown, AddressOf myTreeView_MouseDown Me.Controls.Add(myTreeView) End Sub 'New <STAThreadAttribute()> _ Shared Sub Main() Application.Run(New TreeViewOwnerDraw()) End Sub 'Main ' Draws a node. Private Sub myTreeView_DrawNode(ByVal sender As Object, _ ByVal e As DrawTreeNodeEventArgs) Handles myTreeView.DrawNode Dim NodeImgColl As Image = Image.FromFile("C:\DotNet\FED v6.0\FED v1.0\Resources\Arbo+.gif") Dim NodeImgExp As Image = Image.FromFile("C:\DotNet\FED v6.0\FED v1.0\Resources\Arbo-.gif") Dim x As Integer = e.Node.Bounds.Left - 16 Dim y As Integer = e.Node.Bounds.Top + 1 ' Dessin du Noeud + Texte If e.Node.Level <> 1 Then If e.Node.IsExpanded Then e.Graphics.DrawImage(NodeImgExp, x, y, 12, 12) Else e.Graphics.DrawImage(NodeImgColl, x, y, 12, 12) End If End If ' Draw the background and node text for a selected node. If (e.State And TreeNodeStates.Selected) <> 0 Then ' Draw the background of the selected node. The NodeBounds ' method makes the highlight rectangle large enough to ' include the text of a node tag, if one is present. e.Graphics.FillRectangle(Brushes.White, e.Node.Bounds) ' Draw the node text. e.Graphics.DrawString(e.Node.Text, NodeFont, Brushes.Gray, e.Bounds.Left - 2, e.Bounds.Top) ' Use the default background and node text. Else e.Graphics.DrawString(e.Node.Text, NodeFont, Brushes.Black, e.Bounds.Left - 2, e.Bounds.Top) End If End Sub 'myTreeView_DrawNode Private Sub myTreeView_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Dim clickedNode As TreeNode = myTreeView.GetNodeAt(e.X, e.Y) If Not IsNothing(clickedNode) Then If NodeBounds(clickedNode).Contains(e.X, e.Y) Then myTreeView.SelectedNode = clickedNode End If End If End Sub Private Function NodeBounds(ByVal node As TreeNode) As Rectangle ' Set the return value to the normal node bounds. Dim bounds As Rectangle = node.Bounds Return bounds End Function End Class
La seule différence est donc que dans le 1er cas le TreeView est généré par code tandis que dans le second cas il est mis à la main (avec le même nom.)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88 Imports System Imports System.Drawing Imports System.Windows.Forms Public Class Form1 Private NodeFont As New Font("Arial", 9.0!) Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ' Create and initialize the TreeView control. myTreeView.ShowLines = False ' Add nodes to the TreeView control. Dim node As TreeNode Dim x As Integer For x = 1 To 3 ' Add a root node to the TreeView control. node = myTreeView.Nodes.Add(String.Format("Task {0}", x)) Dim y As Integer For y = 1 To 3 ' Add a child node to the root node. node.Nodes.Add(String.Format("Subtask {0}", y)) Next y Next x myTreeView.DrawMode = TreeViewDrawMode.OwnerDrawText End Sub ' Draws a node. Private Sub myTreeView_DrawNode(ByVal sender As Object, ByVal e As DrawTreeNodeEventArgs) Handles myTreeView.DrawNode Dim NodeImgColl As Image = Image.FromFile("C:\DotNet\FED v6.0\FED v1.0\Resources\Arbo+.gif") Dim NodeImgExp As Image = Image.FromFile("C:\DotNet\FED v6.0\FED v1.0\Resources\Arbo-.gif") Dim x As Integer = e.Node.Bounds.Left - 16 Dim y As Integer = e.Node.Bounds.Top + 1 ' Dessin du Noeud + Texte If e.Node.Level <> 1 Then If e.Node.IsExpanded Then e.Graphics.DrawImage(NodeImgExp, x, y, 12, 12) Else e.Graphics.DrawImage(NodeImgColl, x, y, 12, 12) End If End If ' Draw the background and node text for a selected node. If (e.State And TreeNodeStates.Selected) <> 0 Then ' Draw the background of the selected node. The NodeBounds ' method makes the highlight rectangle large enough to ' include the text of a node tag, if one is present. e.Graphics.FillRectangle(Brushes.White, e.Node.Bounds) ' Draw the node text. e.Graphics.DrawString(e.Node.Text, NodeFont, Brushes.Gray, e.Bounds.Left - 2, e.Bounds.Top) ' Use the default background and node text. Else e.Graphics.DrawString(e.Node.Text, NodeFont, Brushes.Black, e.Bounds.Left - 2, e.Bounds.Top) End If End Sub 'myTreeView_DrawNode Private Sub myTreeView_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles myTreeView.MouseDown Dim clickedNode As TreeNode = myTreeView.GetNodeAt(e.X, e.Y) If Not IsNothing(clickedNode) Then If NodeBounds(clickedNode).Contains(e.X, e.Y) Then myTreeView.SelectedNode = clickedNode End If End If End Sub Private Function NodeBounds(ByVal node As TreeNode) As Rectangle ' Set the return value to the normal node bounds. Dim bounds As Rectangle = node.Bounds Return bounds End Function End Class
CEPENDANT, dans cette dernière configuration, lorsque je clique sur le "+" du second noeud (ou du 3ème...), le nom "SubTask2" vient s'inscrire sous le "+" du premier noeud...
Et le problème disparait si je mets myTreeView.ExpandsAll() dans mon code juste après avoir rempli le TreeView...
Merci de votre aide.
Partager