Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Change backcolor for each row in combo box

Posted on 2004-04-27
6
Medium Priority
?
2,605 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 750 total points
ID: 10928720
0
 
LVL 3

Expert Comment

by:Barkis_
ID: 10928820
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
ID: 10929557
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 5

Expert Comment

by:tgannetts
ID: 10929596
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
ID: 10939977
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
ID: 22360061
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…

971 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