Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Filling a Combo Box column with a different color. 2

Status
Not open for further replies.

dollarbillg

Programmer
Dec 3, 2002
12
0
0
US
Hi,
I have a combo Box filled with data. What I want to do is have every other column in the box to have a BackColor to Yellow. I am able to change the entire Backcolor but not the individual columns.

Any all suggestions are helpful.

Dollarbillg
 
You need to define the combobox as OwnerDrawn (set DrawMode to OwnerDrawFixed or OwnerDrawVariable). Then in the DrawItem event you can do what you want. Here's a quick example using a fixed combobox with 6 items. Note: If you're using a bound combobox, then getting the text values will be slightly different, but nothing too hard to figure out.

'Define the combobox
Friend WithEvents cb As System.Windows.Forms.ComboBox
Me.cb = New System.Windows.Forms.ComboBox()
Me.cb.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed
Me.cb.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
Me.cb.Items.AddRange(New Object() {"Line 1", "Line 2", "Line 3", "Line 4", "Line 5", "Line 6"})


'DrawItem event, set every other item to a yellow background
Private Sub cb_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles cb.DrawItem
If e.Index = -1 Then Exit Sub

'If a list item (not editbox and not selected item) and it's an odd row
If Not (e.State And DrawItemState.ComboBoxEdit) And _
Not (e.State And DrawItemState.Selected) And _
(e.Index Mod 2 = 0) Then
'Draw a yellow background
e.Graphics.FillRectangle(New SolidBrush(Color.LightYellow), e.Bounds)
Else
'Draw its normal background
e.Graphics.FillRectangle(New SolidBrush(e.BackColor), e.Bounds)
End If
'Write the text to the screen using its normal font/forecolor
e.Graphics.DrawString(sender.items(e.Index).ToString, e.Font, _
New SolidBrush(e.ForeColor), e.Bounds.X, e.Bounds.Y)
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top