Solved

Change backcolor for each row in combo box

Posted on 2004-04-27
6
2,594 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
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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

860 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