|
-
May 19th, 2013, 07:31 PM
#1
Thread Starter
Lively Member
[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
-
May 19th, 2013, 08:51 PM
#2
Re: Font Size Issue With Custom Created Listbox Control
 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
-
May 19th, 2013, 09:53 PM
#3
Thread Starter
Lively Member
Re: Font Size Issue With Custom Created Listbox Control
I think it can do the thing but there is a problem:
-
May 19th, 2013, 10:58 PM
#4
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.
-
May 19th, 2013, 11:25 PM
#5
Thread Starter
Lively Member
Re: Font Size Issue With Custom Created Listbox Control
got it. thanks it is working.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|