1 Attachment(s)
[RESOLVED] ListBox Bounds are incorrect
Hello,
So I've finally moved into the VB.NET world and have a quick question. I am drawing a ListBox control with DrawItem event. The problem I am having is that the e.bounds region does not match my text. I changed the font of my ListBox to a larger one with ListBox.Font = New Font(...). Below is my code, when the text is printed it overlaps, slightly, with other rows. Also, I did change the DrawMode to OwnerDrawFixed. When I click the list box, I get a focus rectangle and my highlighted background, but they are displayed as if a default (smaller) font is is used. I've attached a picture to show you.
Code:
Private Sub lstScript_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles lstScript.DrawItem
Dim MyBrush As Brush = Brushes.LightGray
'e.DrawBackground()
If (e.State = (DrawItemState.Focus Or DrawItemState.Selected)) Then
e.Graphics.FillRectangle(SelectColor, e.Bounds)
Else
e.Graphics.FillRectangle(Brushes.White, e.Bounds)
End If
e.Graphics.FillRectangle(MyBrush, 0, e.Bounds.Top, 20, ScriptFont.Height)
e.Graphics.DrawString(lstScript.Items(e.Index).ToString(), e.Font, Brushes.Black, e.Bounds.X + 20, e.Bounds.Y)
e.DrawFocusRectangle()
End Sub
Re: ListBox Bounds are incorrect
try increasing the ItemHeight property of the listbox.
kevin
Re: ListBox Bounds are incorrect
Wow, so simple and yet I never would of thought of changing that. I figured that property would have been updated once the new font was entered.
Thank you.