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 ???
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
- Imports System.IO
- Public Class BackupWatchdog
- Dim PfadDatum As String ' Pfad zur Datumdatei
- Dim fstr As FileStream
- Private Sub cmdPfad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPfad.Click
- If OpenFileDialog.ShowDialog = DialogResult.OK Then
- PfadDatum = OpenFileDialog.FileName ' in der Vareable "Pfad" wird Dateipfad gespeichert
- End If
- PfadSchreiben(PfadDatum)
- End Sub
- Private Sub BackupWatchdog_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
- PfadDatum = PfadLesen()
- If PfadDatum = "" Then
- ListBox.Items.Add(" Pfad zur Datumdatei eingeben !")
- ListBox.Items.Add(" Datum manuell mit Check pruefen !")
- End If
- End Sub
- Private Sub ListBox_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox.DrawItem
- ListBox.DrawMode = DrawMode.OwnerDrawFixed
- ' Draw the background of the ListBox control for each item.
- e.DrawBackground()
- ' Define the default color of the brush as black.
- Dim myBrush As Brush
- ' Determine the color of the brush to draw each item based on the index of the item to draw.
- Select Case (e.Index)
- Case 0
- myBrush = Brushes.Red
- Case 1
- myBrush = Brushes.Orange
- Case 2
- myBrush = Brushes.Green
- End Select
- ' Draw the current item text based on the current Font and the custom brush settings.
- e.Graphics.DrawString(ListBox.Items(e.Index), e.Font, myBrush, New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))
- ' If the ListBox has focus, draw a focus rectangle around the selected item.
- e.DrawFocusRectangle()
- End Sub
- End Class