Solved

Change backcolor for each row in combo box

Posted on 2004-04-27
6
2,593 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with AsEnumerable(), LINQ 4 39
Variable Event ? 3 27
Get Client IP on RDS - VB.NET 15 45
Call an IPL program from vb.net and pass in a file location parmeter 2 19
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

770 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