Warum nicht so?
Code in ein neues Winforms-Formular kopieren
Datenbindung einer Liste (myData) an eine Listbox (lboData),
deren Items per Code gezeichnet werden (DrawItem-Event)
Im Beispiel werden neue Daten per Random im Timer-Event ergänzt.
Public Class frmRunningList
Dim WithEvents lboData As New ListBox With
{.Parent = Me, .Top = 10, .Left = 10, .Width = 100, .Height = 300,
.DrawMode = DrawMode.OwnerDrawFixed}
Dim bs As New BindingSource 'für steuerbare Datenbindung
Dim myData As New List(Of Decimal) 'Daten-Liste
Dim WithEvents timData As New Timer With {.Interval = 1000}
Dim rndm As New Random(123456) 'Datenlieferant
Private Sub frmRunningList_Load(sender As Object, e As EventArgs) _
Handles MyBase.Load
'Datenbindung einrichten
bs.DataSource = myData
lboData.DataSource = bs
timData.Enabled = True
End Sub
Private Sub timData_Tick(sender As Object, e As EventArgs)_
Handles timData.Tick
'Listenlänge auf 10 begrenzen
If myData.Count = 10 Then myData.RemoveAt(0)
'neuen Zufallswert anfügen an Liste
myData.Add(CDec(Math.Round(rndm.NextDouble, 2)))
'Datenbindung der Daten-Liste an die Box aktualisieren
bs.ResetBindings(False)
'aktuellen Mittelwert berechnen
Dim mw As Decimal
For i As Integer = 0 To myData.Count - 1
mw += myData(i)
Next i
mw /= myData.Count
Me.Text = "Mittel: " & Math.Round(mw, 2).ToString
End Sub
Private Sub lboData_DrawItem(sender As Object,
e As DrawItemEventArgs) Handles lboData.DrawItem
If e.Index < 0 Then Exit Sub
'Item in verschiedenen Farben zeichnen
e.DrawBackground()
Dim txt As String = myData(e.Index).ToString
'farbige Pinsel erstellen
Using redbrush As New SolidBrush(Color.Red),
greenbrush As New SolidBrush(Color.Green)
'Text-Farbe anhand der Datenausprägung wählen
'und an die richtige Stelle zeichnen (e.bounds)
If CDec(txt) > 0.5 Then
e.Graphics.DrawString(txt & " ", lboData.Font, greenbrush,
e.Bounds.Location)
Else
e.Graphics.DrawString(txt & " ", lboData.Font, redbrush,
e.Bounds.Location)
End If
End Using
End Sub
End Class
Beitrag wurde zuletzt am 26.07.21 um 13:51:03 editiert. |