Solved

Change backcolor for each row in combo box

Posted on 2004-04-27
6
2,596 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.Net - Not Sure if Multi-threading is What I Need 4 56
visual studio vb.net windows onkeyup 2 40
GUID's in SQL Server 4 31
EMAIL RANGES BASED ON CURRENT TIME 12 41
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 …
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

738 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