开发者

add icon to listbox vb.net

开发者 https://www.devze.com 2023-02-18 13:04 出处:网络
please, how do i draw image in listbox_DrawItem event by the left side i already read throught this code, buts its not helping me

please,

how do i draw image in listbox_DrawItem event by the left side

i already read throught this code, buts its not helping me

Dim targetsize As New Size(16, 16)
Dim img As Image = Nothing
img = My.Resources._error
e.Graphics.DrawImage(img, targetsize)
e.Graphics.DrawString(lsbLog.Items(e.Index).ToString(), _
                              e.Font, mybrush, e.Bounds, StringFormat.GenericDefault)

this is my current code

EDIT

i added your code with some other code, and i get a garbled text

this is part of the code in the DrawItem event

'//Here it draws the border depeding on it's state (the listbox item)
        e.Graphics.DrawRectangle(myPen, e.Bounds.X + 16, e.Bounds.Y, _
                                 e.Bounds.Width - 16, e.Bounds.Height)
        Using b As New SolidBrush(e.ForeColor)
            e.Graphics.DrawString(lsbLog.GetItemText(lsbLog.Items(e.Index)), e.Font, b, e.Bounds)
        End Using
        e.Graphics.DrawImage(img, New Rectangle(e.Bounds.Width - 15, e.Bounds.Y, 12, 12))
        '// Draw the current item text based on the current 
        '// Font and the custom brush settings.
        e.Graphics.DrawString(lsbLog.Items(e.Index).ToString(), e.Font, mybrush, _
                               New Rectangle(e.Bounds.X - 20, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height), _
                               StringFormat.GenericDefault)

This is the code in the MeasureItem event

 Private Sub lsbLog_MeasureItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles lsbLog.MeasureItem
        Dim g As Graphics = e.Graphics
        'We will get the size of the string which we are about to draw,
        'so that we could set the ItemHeight and ItemWidth property
        Dim size As SizeF = g.MeasureString(lsbLog.Items.Item(e.Index).ToString, Me.Font, _
        lsbLog.Width - (5 + SystemInformation.VerticalScrollBarWidth))
        e.ItemHeight = CInt(size.Height) + 5
        e.ItemWidth = CInt(size.Width) + 5
    End Sub

I get a garbeled text and ima开发者_开发问答ge

add icon to listbox vb.net


Two points come to my attention:

  • Did you set the DrawMode to DrawMode.OwnerDrawFixed or DrawMode.OwnerDrawVariable, as stated in the documentation?

  • You seem to be drawing the text directly over the image. Why do you use e.Bounds in DrawString instead of a rectangle that starts a little bit to the right? E.g. something like:

    Dim rect As New Rectangle(e.Bounds.X + 16, e.Bounds.Y, _
                              e.Bounds.Width - 16, e.Bounds.Height)
    ' use rect instead of e.Bounds in DrawString
    

BTW, you shouldn't forget to call DrawBackground and DrawFocusRectange as seen in the example in the documentation.


try this related post, you only need to edit a bit of code, based on the font size, I only needed it with 8.25pt font size, when the font size is larger the icon it's mess up, it doesn't lined up on the font height center

Highlighting Listbox Item when Mouseover on Item

0

精彩评论

暂无评论...
验证码 换一张
取 消