Results 1 to 5 of 5

Thread: [RESOLVED] Font Size Issue With Custom Created Listbox Control

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2009
    Posts
    99

    Resolved [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.

    Name:  Lstbox.png
Views: 593
Size:  3.0 KB
    PHP Code:
    Public Class UrduListBox
        Inherits ListBox
        
    Public Sub New()
            
    Me.DrawMode DrawMode.OwnerDrawVariable
            AddHandler Me
    .DrawItemAddressOf Draw_DrawItem
        End Sub
        
    Public Sub Draw_DrawItem(ByVal sender As ObjectByVal 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.RegularGraphicsUnit.Point)
                    
    e.Graphics.DrawString(sender.Items(e.Index).ToString(), fntbre.Boundssf)
                
    End Using
            
    Catch ex As Exception

            End 
    Try
        
    End Sub 

  2. #2
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Font Size Issue With Custom Created Listbox Control

    Quote Originally Posted by hackerspk View Post
    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

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Dec 2009
    Posts
    99

    Re: Font Size Issue With Custom Created Listbox Control

    I think it can do the thing but there is a problem:

    Name:  error.png
Views: 446
Size:  24.6 KB

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Dec 2009
    Posts
    99

    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
  •  



Click Here to Expand Forum to Full Width