none
Treeview et SelectedImageIndex RRS feed

  • Question

  • Bonjour,

    J'ai un treeview que j'ai charger depuis ma base de donnée et je lui ai assigner des images a l'aide de Imagelist , et dans l’événement double clique du treeview je change l'image du node, mais le problème c'est que dès que je sélectionne un autre node, l'ancien node récupère son image original.

    Voila mon code : 
    Imports MySql.Data.MySqlClient
    Imports MyDll.MyDll
    
    Public Class FrmUser
        Dim MyCmd As MySqlCommand
        Dim MyDr As MySqlDataReader
        Dim Requette As String
        Dim z1, z2, z3, z4, z5, z6, z7, z8, z9 As String
    
        Private Sub FrmUser_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Try
                Requette = "select distinct nom from usertable where noeud<>'x' order by nom"
                MyCmd = New MySqlCommand(Requette, MyDb)
                MyDr = MyCmd.ExecuteReader
                While MyDr.Read()
                    List1.Items.Add(MyDr.GetValue(0).ToString())
                End While
                MyDr.Close()
    
                RemplirTreeView()
    
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Application Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End Sub
    
        Private Sub List1_Click(sender As Object, e As EventArgs) Handles List1.SelectedIndexChanged
            TxtUser.Text = List1.SelectedItem
            TxtUser.Enabled = False
            RemplirTreeView()
        End Sub
    
        Private Sub RemplirTreeView()
            Try
                TreeView1.Nodes.Clear()
                If TxtUser.Text = "" And TxtPwd.Text = "" Then Exit Sub
                TxtPwd.Text = ""
                Requette = "select distinct pass from usertable where  nom='" & TxtUser.Text & "' and app='" & My.Application.Info.AssemblyName & "' and autoris<>3"
                MyCmd = New MySqlCommand(Requette, MyDb)
                MyDr = MyCmd.ExecuteReader
                While MyDr.Read()
                    TxtPwd.Text = MyDr.GetValue(0).ToString()
                End While
                MyDr.Close()
    
                Requette = "select * from usertable where app='" & My.Application.Info.AssemblyName & "' and nom='" & TxtUser.Text & "' order by cle"
                MyCmd = New MySqlCommand(Requette, MyDb)
                MyDr = MyCmd.ExecuteReader
                    While MyDr.Read()
                        Dim wCle As String = MyDr.GetValue(4).ToString()
                        Dim wCaption As String = MyDr.GetValue(6).ToString()
                        Dim wAutoris As Integer = MyDr.GetValue(5) - 1
                        z1 = wCle.Substring(0, 2)
                        With TreeView1
                            If wCle.Length = 2 Then
                                .Nodes.Add(wCle, wCaption, wAutoris, wAutoris)
                            ElseIf wCle.Length = 3 Then
                                .Nodes(z1).Nodes.Add(wCle, wCaption, wAutoris, wAutoris)
                            ElseIf wCle.Length = 4 Then
                                z2 = wCle.Substring(0, 3)
                                .Nodes(z1).Nodes(z2).Nodes.Add(wCle, wCaption, wAutoris, wAutoris)
                            ElseIf wCle.Length = 5 Then
                                z3 = wCle.Substring(0, 4)
                                .Nodes(z1).Nodes(z2).Nodes(z3).Nodes.Add(wCle, wCaption, wAutoris, wAutoris)
                            ElseIf wCle.Length = 6 Then
                                z4 = wCle.Substring(0, 5)
                                .Nodes(z1).Nodes(z2).Nodes(z3).Nodes(z4).Nodes.Add(wCle, wCaption, wAutoris, wAutoris)
                            ElseIf wCle.Length = 7 Then
                                z5 = wCle.Substring(0, 6)
                                .Nodes(z1).Nodes(z2).Nodes(z3).Nodes(z4).Nodes(z5).Nodes.Add(wCle, wCaption, wAutoris, wAutoris)
                            ElseIf wCle.Length = 8 Then
                                z6 = wCle.Substring(0, 7)
                                .Nodes(z1).Nodes(z2).Nodes(z3).Nodes(z4).Nodes(z5).Nodes(z6).Nodes.Add(wCle, wCaption, wAutoris, wAutoris)
                            ElseIf wCle.Length = 9 Then
                                z7 = wCle.Substring(0, 8)
                                .Nodes(z1).Nodes(z2).Nodes(z3).Nodes(z4).Nodes(z5).Nodes(z6).Nodes(z7) _
                                    .Nodes.Add(wCle, wCaption, wAutoris, wAutoris)
                            ElseIf wCle.Length = 10 Then
                                z8 = wCle.Substring(0, 9)
                                .Nodes(z1).Nodes(z2).Nodes(z3).Nodes(z4).Nodes(z5).Nodes(z6).Nodes(z7) _
                                    .Nodes(z8).Nodes.Add(wCle, wCaption, wAutoris, wAutoris)
                            ElseIf wCle.Length = 10 Then
                                z9 = wCle.Substring(0, 10)
                                .Nodes(z1).Nodes(z2).Nodes(z3).Nodes(z4).Nodes(z5).Nodes(z6).Nodes(z7) _
                                    .Nodes(z8).Nodes(z9).Nodes.Add(wCle, wCaption, wAutoris, wAutoris)
                            End If
                        End With
                    End While
                MyDr.Close()
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Application Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End Sub
        
        Private Sub TreeView1_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles TreeView1.MouseDoubleClick, TreeView1.LostFocus
    
            Dim i As Integer = TreeView1.SelectedNode.SelectedImageIndex
            i = i - 1
            If i < 0 Then i = 2
            TreeView1.SelectedNode.SelectedImageIndex = i
    
        End Sub
    
    End Class

    Merci d'avance
    jeudi 31 mai 2018 15:05

Réponses

Toutes les réponses