Solved

Change backcolor for each row in combo box

Posted on 2004-04-27
6
2,588 Views
Last Modified: 2009-07-29
Is there a way to have a combo box and have each row different colors?  For example I have a table that has codes in it and each code has  a field for a highlight color.  I want to be able to have these loaded into a combobox and have the back color the highlight color.

Example:

C - backcolor would be orange
C - backcolor would be yellow
O - backcolor white
O - backcolor blue


Is this possible?

Thanks
Stacy
0
Comment
Question by:stanuf
6 Comments
 
LVL 25

Accepted Solution

by:
RonaldBiemans earned 250 total points
Comment Utility
0
 
LVL 3

Expert Comment

by:Barkis_
Comment Utility
No, unfortunattely this is not possible in a combobox.

There is only one type of control that can show data with alternating row colours and that is a Datagrid.
If you read the lines in your table and interpret the colour codes  then you might be able to come up with some code that sets the background colour and the alternating background colour according to the first two values in your table. The result will be a datagrid with the background alternating  between the two colours.
But think that is as close as you'll be able to get.

/Barkis_

0
 
LVL 5

Expert Comment

by:tgannetts
Comment Utility
It is possible to change the colors for rows in the drop down list of a combo box by handling the drawing yourself:

The following example sets the colour of the row according to the colour name held in the text in the row.

First, you have to make sure that the DrawMode property for the combo box is set to OwnerDrawFixed, not normal.

Then you can handle the DrawItem event raised when the drop down is raised.

Add the following event handler code:

Imports System.Drawing

...

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

        Dim c As System.Drawing.Color

        'Sometimes calls this procedure with an index of -1
        If e.Index < 0 Then
            e.DrawBackground()
            e.DrawFocusRectangle()
            Exit Sub
        End If

        'Determine colour to use
        Select Case Me.ComboBox1.Items.Item(e.Index)
            Case "Blue"
                c = Color.FromName("Blue")
            Case "Green"
                c = Color.FromName("Green")
            Case "Orange"
                c = Color.FromName("Orange")
            Case "Red"
                c = Color.FromName("Red")
            Case Else
                c = Color.FromName("White")
        End Select

        'Carry out necessary procedures
        e.DrawBackground()
        e.DrawFocusRectangle()

        'Create rectangle, fill it with desired colour, and add text
        e.Graphics.DrawRectangle(New Pen(c), New Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))
        e.Graphics.FillRectangle(New SolidBrush(c), New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))
        e.Graphics.DrawString(Me.ComboBox1.Items.Item(e.Index), Me.ComboBox1.Font, New SolidBrush(Color.Black), New   RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))
 
End Sub

To do what you wish to do, you just have to replace the Select case section in the above code with your own logic to determine which colour to fill the rectangle with.

Hope this is of some help.

Tom
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 5

Expert Comment

by:tgannetts
Comment Utility
BTW this will only update the drop down list section of the combo box.

To update the static text part, you will have to handle the SelectedIndexChanged event, and set the BackColor property of the ComboBox according to the same logic you use in the DrawItem field.

So:

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

Dim c As System.Drawing.Color

'Determine colour to use
Select Case Me.ComboBox1.Items.Item(e.Index)
     Case "Blue"
          c = Color.FromName("Blue")
     Case "Green"
          c = Color.FromName("Green")
     Case "Orange"
          c = Color.FromName("Orange")
     Case "Red"
          c = Color.FromName("Red")
     Case Else
          c = Color.FromName("White")
End Select

Me.ComboBox1.BackColor = c

End Sub

Tom.
0
 
LVL 14

Expert Comment

by:ptakja
Comment Utility
You CAN do this with the Infragistics UltraCombo box.  Check them out at http://www.infragistics.com.

No, I don't work for Infragistics, however I have been using their stuff for 2 yrs now.  GREAT tools!
0
 

Expert Comment

by:mpaver
Comment Utility
The nearest I got was as follows:
With ComboBox1
    If ComboBox1.Value = "name1" Then
        .BackColor = RGB(87, 68, 90)
    ElseIf ComboBox1.Value = "Name2" Then
        .BackColor = RGB(88, 127, 3)
    End If
End With

But it only changes the colour of the whole box. Maybe it can be edited to do row by row as my technical abilities aren't exactly amazing.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now