ComboBox Colors

  • Thread starter Thread starter Sumi
  • Start date Start date
S

Sumi

Hi,

I have the following code for combobox:

CB1.additem("One") 'Red Color
CB1.additem("two") 'Blue Color
CB1.additem("three") 'Green Color
CB1.additem("four") 'Yellow Color

I need to add different color to these items. Is there a way to do it?

Thanks

-Sonu
 
Hi,

Set the comboboxes Drawmode to ownerdrawnfixed and draw the item
yourself. Here is an example.

Dim arColor() As String

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

ComboBox1.DrawMode = DrawMode.OwnerDrawFixed

ComboBox1.Location = New Point(60, 60)

ComboBox1.Width = 150

ComboBox1.Visible = True

ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList

arColor = KnownColor.GetNames(GetType(KnownColor))

ComboBox1.Items.AddRange(arColor)

Me.Text = "Ownerdrawn Combobox"

End Sub



Private Sub combobox1_DrawItem(ByVal sender As Object, ByVal e As
System.Windows.Forms.DrawItemEventArgs) Handles ComboBox1.DrawItem

Dim g As Graphics = e.Graphics

Dim s As String

Dim d As Date

Dim dr As DataRowView

Dim dt As Date

Dim br As Brush = SystemBrushes.WindowText

Dim brBack As Brush

Dim rDraw As Rectangle

Dim bSelected As Boolean = CBool(e.State And DrawItemState.Selected)

Dim bValue As Boolean = CBool(e.State And DrawItemState.ComboBoxEdit)

rDraw = e.Bounds

rDraw.Inflate(-1, -1)

If bSelected And Not bValue Then

brBack = Brushes.LightBlue

g.FillRectangle(Brushes.LightBlue, rDraw)

g.DrawRectangle(Pens.Blue, rDraw)

Else

brBack = Brushes.White

g.FillRectangle(brBack, e.Bounds)

End If

br = Nothing

brBack = Nothing

rDraw = Nothing

Try

s = ComboBox1.Items.Item(e.Index).ToString

Catch

s = ""

End Try

Dim x, y As Integer

x = e.Bounds.Left + 25

y = e.Bounds.Top + 1

Dim c As Color

Dim b As SolidBrush

c = Color.FromName(s)

b = New SolidBrush(c)

g.FillRectangle(b, x - 20, y + 2, 10, 10)

g.DrawString(s, ComboBox1.Font, Brushes.Black, x, y)

End Sub



Ken

------------------------

Hi,

I have the following code for combobox:

CB1.additem("One") 'Red Color
CB1.additem("two") 'Blue Color
CB1.additem("three") 'Green Color
CB1.additem("four") 'Yellow Color

I need to add different color to these items. Is there a way to do it?

Thanks

-Sonu
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top