0

Hi guys. I'm stuck. How can I use a custom color (rgb 25, 25, 25)(for example) as my fill rectangle color instead of the default blue?

    'Private Sub ListBox10_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox10.DrawItem
    '    e.DrawBackground()
    '    If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then
    '        e.Graphics.FillRectangle(Color.??, e.Bounds)
    '    End If
    '    Using b As New SolidBrush(e.ForeColor)
    '        e.Graphics.DrawString(ListBox10.GetItemText(ListBox10.Items(e.Index)), e.Font, b, e.Bounds)
    '    End Using
    '    e.DrawFocusRectangle()
    'End Sub

Edited by o Chantelle o: elaboration

Attachments Captkure.PNG 0.75 KB
3
Contributors
7
Replies
25
Views
3 Years
Discussion Span
Last Post by o Chantelle o
1

FillRectangle expects a Brush not a Color, but if you instantiate a Brush derivative(i.e. SolidBrush), you can set the color property using the Color.FromARGB overload that accepts just the RGB values and assume and Alpaha value of 255 (fully opaque).

    Private Sub ListBox10_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox10.DrawItem
        Dim mybrush As System.Drawing.SolidBrush
        mybrush.Color = Color.FromArgb(25, 25, 25)
        e.DrawBackground()
        If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then
            e.Graphics.FillRectangle(mybrush, e.Bounds)
        End If
        Using b As New SolidBrush(e.ForeColor)
            e.Graphics.DrawString(ListBox10.GetItemText(ListBox10.Items(e.Index)), e.Font, b, e.Bounds)
        End Using
        e.DrawFocusRectangle()
    End Sub

Edited by tinstaafl

0

@tinstaafl How would I go about using a custom color and not a default built-in one? Like for example, I dont know the name for RGB 25,25,25.

1

Sorry I hit the wrong button, I've edited it for a better explanation. Of course you can use the overload that accepts the Alpha value and set it for whatever value suits you.

Edited by tinstaafl

1

Not all colors have a name. With all the combinations of 255,255,255 integers you can form more than 1,5 million colors. There is however an enumeration that lists all the colors that have a name Click Here

0

Thanx for the help guys. Im close, one small warning: "Variable mybrush is used before its assigned a value"
Any help with this?

Edited by o Chantelle o

1

Sorry wasn't able to actually build the code. Try this instead: Dim mybrush As New System.Drawing.SolidBrush(Color.FromArgb(25, 25, 25))

Edited by tinstaafl

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.