viernes, 8 de agosto de 2008

How to brushes in ComboBox - Como dar color a los Items de un ComboBox con .Net

Hola de nuevo quizas algunos se hayan preguntado si se puede asignar color a algunos items de un combobox, la respuesta es Claro que se puede, lo que tenemos que establecer es la propiedad DrawMode del combo en la opcion OwnerDrawVariable

Después utilizaremos el evento DrawItem para colorear el fondo y dibujar el texto.

'Evento
Private Sub ComboBox1_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ComboBox1.DrawItem
Select Case e.Index
Case 0,1,2,3,4 'Amarillo
e.Graphics.FillRectangle(Brushes.Yellow , e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height)
Case 5,6,7,8 'Verde
e.Graphics.FillRectangle(Brushes.Green, e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height)
End Select
'Coloreamo el texto
e.Graphics.DrawString(ComboBox1.Items(e.Index).ToString(), e.Font, New SolidBrush(e.ForeColor), e.Bounds)
End Sub

Espero les sea de utilidad, hasta la proxima.
'----------------------------------------------------------------------------------------
Hello again perhaps some have questioned whether we can assign color to some items from a combobox, the answer is "Obviously, you can, what we have to establish is the DrawMode property of the combo in the option OwnerDrawVariable

Then use the DrawItem event coloring the background and draw the text.


'Event
Private Sub ComboBox1_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ComboBox1.DrawItem
Select Case e.Index
Case 0,1,2,3,4 'Yellow
e.Graphics.FillRectangle(Brushes.Yellow , e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height)
Case 5,6,7,8 'Green
e.Graphics.FillRectangle(Brushes.Green, e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height)
End Select
'Coloring the text
e.Graphics.DrawString(ComboBox1.Items(e.Index).ToString(), e.Font, New SolidBrush(e.ForeColor), e.Bounds)
End Sub

I hope will be useful until the next one