owner drawn listbox

modularbeing

Regular
Joined
Jan 8, 2004
Messages
63
I am trying to get a little fancy with a list box and use the owner drawn feature to change the look. I have written some code to change the look of the selected item but i cannot figure out how I can reverse it when someone clicks another item.

Here is the code I wrote to do this:
Code:
Private Sub listBox1_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
        e.DrawBackground()
        Dim l As ListBox = CType(sender, ListBox)

        Dim stringSize As SizeF = e.Graphics.MeasureString(l.SelectedItem, New Font(FontFamily.GenericSansSerif, 14, FontStyle.Bold))

        If ((DrawItemState.Selected) > 0) Then
            Dim selRect As New Rectangle(e.Bounds.X, e.Bounds.Y, l.Width - 1, stringSize.Height / 2)
            Dim br As New SolidBrush(Color.FromArgb(206, 223, 255))
            e.Graphics.FillRectangle(br, selRect)
            e.Graphics.DrawRectangle(New Pen(Color.FromArgb(30, 108, 253), 1), selRect)
            e.Graphics.DrawString(l.Items(e.Index), e.Font, Brushes.Black, New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))
            br.Dispose()
        End If
    End Sub
 
You have to draw over 'all' of the elements in an owner-drawn listbox.

I'm quite surprised if
If ((DrawItemState.Selected) > 0) Then
that line works (I thought DrawItemState.Selected is always 1)

But, I'll just assume that it works for now.

You would put an Else onto this If block and then, in this Else block, you draw the object as it would be if it were not selected. :)
 
Iceplug said:
You have to draw over 'all' of the elements in an owner-drawn listbox.

I'm quite surprised if
If ((DrawItemState.Selected) > 0) Then
that line works (I thought DrawItemState.Selected is always 1)

But, I'll just assume that it works for now.

You would put an Else onto this If block and then, in this Else block, you draw the object as it would be if it were not selected. :)

I have tried that before and it does not work.....actually if I remove the if then statement and run it , the program does not draw the rectangles around the text but its still draws the text .
 
OK, so how about doing the check like this. That If statement must not be working as I thought it wouldn't.

If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then
 
Iceplug said:
OK, so how about doing the check like this. That If statement must not be working as I thought it wouldn't.

If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then

awsome, that fixed it! thank you!
now for my next step, which I havnt looked into yet but do you know how to either redraw the vertical scroll or tell the listbox to scroll up or down?
 
Back
Top