1 Attachment(s)
[RESOLVED] Font Size Issue With Custom Created Listbox Control
I am try to Inherits a Listbox control to change StringFormatFlags direction. All is OK, but when I change the font size of Listbox control it dose not draw item properly. How to fix this issue. I am stuck with this problem from many days.
PHP Code:
Public Class UrduListBox
Inherits ListBox
Public Sub New()
Me.DrawMode = DrawMode.OwnerDrawVariable
AddHandler Me.DrawItem, AddressOf Draw_DrawItem
End Sub
Public Sub Draw_DrawItem(ByVal sender As Object, ByVal e As DrawItemEventArgs)
Try
Dim sf As New StringFormat()
If sender.RightToLeft = Windows.Forms.RightToLeft.Yes Then
sf.FormatFlags = StringFormatFlags.DirectionRightToLeft
End If
Dim br As New SolidBrush(sender.ForeColor)
If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then
e.DrawBackground()
br = Brushes.White
Else
e.Graphics.FillRectangle(New SolidBrush(e.BackColor), e.Bounds)
br = Brushes.Black
End If
Using fnt As New Font(sender.Font.Name.ToString(), sender.Font.Size.ToString(), FontStyle.Regular, GraphicsUnit.Point)
e.Graphics.DrawString(sender.Items(e.Index).ToString(), fnt, br, e.Bounds, sf)
End Using
Catch ex As Exception
End Try
End Sub
Re: Font Size Issue With Custom Created Listbox Control
Quote:
Originally Posted by
hackerspk
All is OK, but when I change the font size of Listbox control it dose not draw item properly. How to fix this issue. I am stuck with this problem from many days.
Honestly I'm not sure but I've seen posts that include OnMeasureItem that supposedly fix that sort of thing, quick test,...
Code:
Public Class UrduListBox
Inherits ListBox
Public Sub New()
Me.DrawMode = DrawMode.OwnerDrawVariable
End Sub
Protected Overrides Sub OnMeasureItem(e As MeasureItemEventArgs)
MyBase.OnMeasureItem(e)
Dim ItemSize As SizeF = e.Graphics.MeasureString(Me.Items(e.Index).ToString, Me.Font, Me.ClientSize.Width)
e.ItemHeight = Size.Round(ItemSize).Height
End Sub
Protected Overrides Sub OnDrawItem(e As DrawItemEventArgs)
MyBase.OnDrawItem(e)
e.DrawBackground()
Dim sf As New StringFormat(CType(Me.RightToLeft, StringFormatFlags))
Dim br As New SolidBrush(Me.ForeColor)
If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then
br.Color = Color.White
End If
e.Graphics.DrawString(Me.Items(e.Index).ToString, Me.Font, br, e.Bounds, sf)
e.DrawFocusRectangle()
End Sub
End Class
1 Attachment(s)
Re: Font Size Issue With Custom Created Listbox Control
I think it can do the thing but there is a problem:
Attachment 100339
Re: Font Size Issue With Custom Created Listbox Control
You probably just need to test that there's an item at the specific index, i.e. that the index is less than Items.Count, in the OnMeasureItem method.
Re: Font Size Issue With Custom Created Listbox Control
got it. thanks it is working.