G
Guest
I am owner-drawing a listbox, in an attempt to create a nice list with some
custom "fields" and text layout.
Essentially it works, but I must be missing something big, conceptually,
because I get all kinds of screen artifacts and weirdness.
My general goal is: list item with a few areas for text, every other item
shaded a light color for readability, font color changes with selection. The
listbox is populated with custom structurs loaded with data. Here is my
prototype code so far:
Private Sub lstAccounts_DrawItem(ByVal sender As Object, ByVal e As
System.Windows.Forms.DrawItemEventArgs) Handles lstAccounts.DrawItem'
' Set fonts and colors
Dim cLine As New System.Drawing.Pen(System.Drawing.SystemColors.ControlDark)
Dim fLarge As New Font("Verdana", 12, FontStyle.Bold, GraphicsUnit.Pixel)
Dim fNorm As New Font("Verdana", 10, FontStyle.Regular, GraphicsUnit.Pixel)
Dim fMed As New Font("Verdana", 11, FontStyle.Regular, GraphicsUnit.Pixel)
Dim bNorm As System.Drawing.Brush
' Make Variable Defaults
Dim mX As Single = e.Bounds.Width
Dim mT As Single = e.Bounds.Top
Dim mY As Single = e.Bounds.Height
Dim m75 As Single = mX * 0.75
Dim m50 As Single = mX * 0.5
Dim objA As Account
Dim objS As System.Drawing.SizeF
' Get Object
objA = lstAccounts.Items(e.Index)
' Draw background
e.DrawBackground()
e.DrawFocusRectangle()
' Assign Pen Color
If (e.State = DrawItemState.Selected) Or (e.State = DrawItemState.Selected +
DrawItemState.Focus) Then
bNorm = System.Drawing.Brushes.White
Else
bNorm = System.Drawing.Brushes.Black
End If
' Draw Background if not selected and odd
If e.Index Mod 2 = 1 Then
If (e.State <> DrawItemState.Selected) And (e.State <>
DrawItemState.Selected + DrawItemState.Focus) Then
e.Graphics.FillRectangle(System.Drawing.SystemBrushes.ControlLight,
0, mT, mX, mT + mY)
End If
End If
' Draw Bottom Line
e.Graphics.DrawLine(cLine, 0, mT + mY, mX, mT + mY)
' Draw 75% Line, 50% line
e.Graphics.DrawLine(cLine, m75, mT, m75, mT + mY)
e.Graphics.DrawLine(cLine, m50, mT, m50, mT + mY)
e.Graphics.DrawLine(cLine, m50, mT + (mY / 2), m75, mT + (mY / 2))
' Draw Name of Item, Description
e.Graphics.DrawString(objA.Account_Name, fLarge, bNorm, 5, mT + 5)
e.Graphics.DrawString(objA.Account_Memo, fNorm, bNorm, 5, mT + 20)
' Draw Balance Information (right aligned)
objS = e.Graphics.MeasureString(objA.Account_Balance.ToString("$0.00"), fMed)
e.Graphics.DrawString(objA.Account_Balance.ToString("$0.00"), fMed, bNorm,
mX - objS.Width - 5, mT + 15)
End Sub
Like I said, this code essentially works. Start it up, add some items to
the list box, it shows correctly. However, if you resize the form, or give
focus, or remove focus, or generally interact with it the rendering goes
bannas. What am I missing? Anyone out there with more experience than me?
(Read: any experience in this regard!)
Many thanks!
custom "fields" and text layout.
Essentially it works, but I must be missing something big, conceptually,
because I get all kinds of screen artifacts and weirdness.
My general goal is: list item with a few areas for text, every other item
shaded a light color for readability, font color changes with selection. The
listbox is populated with custom structurs loaded with data. Here is my
prototype code so far:
Private Sub lstAccounts_DrawItem(ByVal sender As Object, ByVal e As
System.Windows.Forms.DrawItemEventArgs) Handles lstAccounts.DrawItem'
' Set fonts and colors
Dim cLine As New System.Drawing.Pen(System.Drawing.SystemColors.ControlDark)
Dim fLarge As New Font("Verdana", 12, FontStyle.Bold, GraphicsUnit.Pixel)
Dim fNorm As New Font("Verdana", 10, FontStyle.Regular, GraphicsUnit.Pixel)
Dim fMed As New Font("Verdana", 11, FontStyle.Regular, GraphicsUnit.Pixel)
Dim bNorm As System.Drawing.Brush
' Make Variable Defaults
Dim mX As Single = e.Bounds.Width
Dim mT As Single = e.Bounds.Top
Dim mY As Single = e.Bounds.Height
Dim m75 As Single = mX * 0.75
Dim m50 As Single = mX * 0.5
Dim objA As Account
Dim objS As System.Drawing.SizeF
' Get Object
objA = lstAccounts.Items(e.Index)
' Draw background
e.DrawBackground()
e.DrawFocusRectangle()
' Assign Pen Color
If (e.State = DrawItemState.Selected) Or (e.State = DrawItemState.Selected +
DrawItemState.Focus) Then
bNorm = System.Drawing.Brushes.White
Else
bNorm = System.Drawing.Brushes.Black
End If
' Draw Background if not selected and odd
If e.Index Mod 2 = 1 Then
If (e.State <> DrawItemState.Selected) And (e.State <>
DrawItemState.Selected + DrawItemState.Focus) Then
e.Graphics.FillRectangle(System.Drawing.SystemBrushes.ControlLight,
0, mT, mX, mT + mY)
End If
End If
' Draw Bottom Line
e.Graphics.DrawLine(cLine, 0, mT + mY, mX, mT + mY)
' Draw 75% Line, 50% line
e.Graphics.DrawLine(cLine, m75, mT, m75, mT + mY)
e.Graphics.DrawLine(cLine, m50, mT, m50, mT + mY)
e.Graphics.DrawLine(cLine, m50, mT + (mY / 2), m75, mT + (mY / 2))
' Draw Name of Item, Description
e.Graphics.DrawString(objA.Account_Name, fLarge, bNorm, 5, mT + 5)
e.Graphics.DrawString(objA.Account_Memo, fNorm, bNorm, 5, mT + 20)
' Draw Balance Information (right aligned)
objS = e.Graphics.MeasureString(objA.Account_Balance.ToString("$0.00"), fMed)
e.Graphics.DrawString(objA.Account_Balance.ToString("$0.00"), fMed, bNorm,
mX - objS.Width - 5, mT + 15)
End Sub
Like I said, this code essentially works. Start it up, add some items to
the list box, it shows correctly. However, if you resize the form, or give
focus, or remove focus, or generally interact with it the rendering goes
bannas. What am I missing? Anyone out there with more experience than me?
(Read: any experience in this regard!)
Many thanks!