Oppure

Loading
02/11/21 13:16
Carlo
Bene, ho rivisto il codice e si vede che è fatto di fretta, ho messo una function, e quando si ridisegna in default, non vengono controllati inutilmente i parametri, lo stesso se il testo è selezioato, non vengono inutilmente scelti i colori.
Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        TVAtt.DrawMode = TreeViewDrawMode.OwnerDrawText
        AddHandler TVAtt.DrawNode, AddressOf TVAtt_drawNode
        '' treeview, fondo bianco scritte nere
        'TVAtt.BackColor = Color.White
        'TVAtt.LineColor = Color.Gray
        'TVAtt.ForeColor = Color.Black
        ' tree view, fondo grigio scritte bianche
        TVAtt.BackColor = Color.FromArgb(60, 60, 60)
        TVAtt.LineColor = Color.Gray
        TVAtt.ForeColor = Color.White
        TVAtt.HideSelection = False ' la selezione sarà visible anche quando la Treeview o il form perde il fuoco
    End Sub

    Private Sub TVAtt_drawNode(sender As Object, e As DrawTreeNodeEventArgs)

        If e.Node Is Nothing Then Exit Sub ' inutile ma per sicurezza...
        Dim selected = e.State And TreeNodeStates.Selected
        Dim g As Graphics = e.Graphics
        Dim colore1 As Brush = New SolidBrush(TVAtt.ForeColor)
        Dim colore2 As Brush = New SolidBrush(TVAtt.ForeColor)
        Dim linea = e.Node.Text.Split(";"c)
        Dim Size As SizeF

        ' se c'è un solo parametro oppure le coordinate non sono aggiornate
        If linea.GetUpperBound(0) = 0 Or e.Node.Bounds.X = 0 Then
            e.DrawDefault = True
            Exit Sub
        End If

        ' interpretazione dei parametri 0, 2 e 3
        If linea.GetUpperBound(0) > 1 Then
            If selected Then
                colore1 = New SolidBrush(Color.White) ' colore del testo1 se selezionato
                ' colore di fondo della selezione
                e.Graphics.FillRectangle(Brushes.DarkBlue, e.Node.Bounds.X, e.Node.Bounds.Y, e.Node.Bounds.Width, e.Node.Bounds.Height)
            Else
                colore1 = sceglicolore(linea(2)) ' colore del testo 1 non selezionato
            End If
            If linea.GetUpperBound(0) > 2 AndAlso linea(3).ToLower.Trim = "true" Then
                Size = g.MeasureString(linea(0), New Font(TVAtt.Font, FontStyle.Bold))
                g.DrawString(linea(0), New Font(TVAtt.Font, FontStyle.Bold), colore1, e.Node.Bounds)
            Else
                Size = g.MeasureString(linea(0), TVAtt.Font)
                g.DrawString(linea(0), TVAtt.Font, colore1, e.Node.Bounds)
            End If
        End If

        ' interpretazione dei parametri 1, 4 e 5
        If linea.GetUpperBound(0) > 3 Then
            If selected Then
                colore2 = New SolidBrush(Color.White) ' colore del testo2 se selezionato
            Else
                colore2 = sceglicolore(linea(4)) ' colore del testo 2 non selezionato
            End If
            If linea.GetUpperBound(0) > 4 AndAlso linea(5).ToLower.Trim = "true" Then
                g.DrawString(linea(1), New Font(TVAtt.Font, FontStyle.Bold), colore2, e.Node.Bounds.X + Size.Width + 2, e.Node.Bounds.Y)
            Else
                g.DrawString(linea(1), TVAtt.Font, colore2, e.Node.Bounds.X + Size.Width + 2, e.Node.Bounds.Y)
            End If
        End If

    End Sub

    Private Function sceglicolore(colore As String) As Brush
        Select Case colore.ToLower.Trim
            Case "red"
                Return Brushes.Red

            Case "gray"
                Return Brushes.Gray

            Case "green"
                Return Brushes.LightGreen

            Case "blue"
                Return Brushes.RoyalBlue

            Case "yellow"
                Return Brushes.Gold

            Case "cyan"
                Return Brushes.Cyan

            Case "magen"
                Return Brushes.Magenta

            Case "orange"
                Return Brushes.Orange

            Case Else
                Return New SolidBrush(TVAtt.ForeColor)
        End Select
    End Function

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim treeNode1 As New TreeNode("PrimoNodo ")
        treeNode1.Nodes.Add(" testo1Arancio;testo2Giallo;orange;true;yellow;true")
        treeNode1.Nodes.Add(" testo1Rosso;testo2Blue;red;;blue;true")
        treeNode1.Nodes.Add(" testo1Nero;testo2Grigio;black;true;gray;")
        TVAtt.Nodes.Add(treeNode1)

        Dim treeNode2 As New TreeNode("SecondoNodo ")
        treeNode2.Nodes.Add(" testo1Verde;testo2Giallo;green;;yellow;")
        treeNode2.Nodes.Add(" testo1Rosso;testo2Blue;red;true;blue;")
        treeNode2.Nodes.Add(" testo1Nero;testo2Grigio;black;;gray;true")
        TVAtt.Nodes.Add(treeNode2)

        TVAtt.Nodes.Add(" testoStandard")
        TVAtt.Nodes.Add(" testoStandard")

        Dim treeNode3 As New TreeNode("TerzoNodo ")
        treeNode3.Nodes.Add(" Testo;123;cyan;;red;")
        treeNode3.Nodes.Add(" Testo;456;cyan;;red;True")
        treeNode3.Nodes.Add(" Testo;789;cyan;;red;True")
        Dim treeNode4 As New TreeNode("QuartoNodo ")
        treeNode4.Nodes.Add(" testo1Verde;testo2Giallo;green;true;yellow;true")
        treeNode4.Nodes.Add(" testo1Rosso;testo2Nero;red;;black;true")
        treeNode4.Nodes.Add(" testo1Magenta;testo2Arancio;magen;true;orange;")
        treeNode3.Nodes.Add(treeNode4)
        TVAtt.Nodes.Add(treeNode3)

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        If TVAtt.Nodes.Count > 2 Then
            TVAtt.Nodes.Item(4).Nodes(3).Text = " TV scadenza;-123-;green;;red;true"
            TVAtt.Nodes.Item(4).ExpandAll()
        End If
    End Sub
End Class
Ultima modifica effettuata da Carlo 02/11/21 13:22
in programmazione tutto è permesso