Farbe von Text in ListBox ändern

  • VB.NET

Es gibt 4 Antworten in diesem Thema. Der letzte Beitrag () ist von SystemUnknow.

    Farbe von Text in ListBox ändern

    Hallo zusammen!

    Ich schreibe eine kleine Programm in der will ich die Farbe von Text in ListBox ändern. Ich habe folgende Code geschrieben, aber es funktioniert leider nicht. Kann mir jemand sagen wo habe ich Fehler ???

    VB.NET-Quellcode

    1. Imports System.IO
    2. Public Class BackupWatchdog
    3. Dim PfadDatum As String ' Pfad zur Datumdatei
    4. Dim fstr As FileStream
    5. Private Sub cmdPfad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPfad.Click
    6. If OpenFileDialog.ShowDialog = DialogResult.OK Then
    7. PfadDatum = OpenFileDialog.FileName ' in der Vareable "Pfad" wird Dateipfad gespeichert
    8. End If
    9. PfadSchreiben(PfadDatum)
    10. End Sub
    11. Private Sub BackupWatchdog_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    12. PfadDatum = PfadLesen()
    13. If PfadDatum = "" Then
    14. ListBox.Items.Add(" Pfad zur Datumdatei eingeben !")
    15. ListBox.Items.Add(" Datum manuell mit Check pruefen !")
    16. End If
    17. End Sub
    18. Private Sub ListBox_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox.DrawItem
    19. ListBox.DrawMode = DrawMode.OwnerDrawFixed
    20. ' Draw the background of the ListBox control for each item.
    21. e.DrawBackground()
    22. ' Define the default color of the brush as black.
    23. Dim myBrush As Brush
    24. ' Determine the color of the brush to draw each item based on the index of the item to draw.
    25. Select Case (e.Index)
    26. Case 0
    27. myBrush = Brushes.Red
    28. Case 1
    29. myBrush = Brushes.Orange
    30. Case 2
    31. myBrush = Brushes.Green
    32. End Select
    33. ' Draw the current item text based on the current Font and the custom brush settings.
    34. e.Graphics.DrawString(ListBox.Items(e.Index), e.Font, myBrush, New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))
    35. ' If the ListBox has focus, draw a focus rectangle around the selected item.
    36. e.DrawFocusRectangle()
    37. End Sub
    38. End Class