Click here to Skip to main content
15,905,913 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello everyone,
I encourter a problem.I want to change the combobox items' background color and its text color according to my choice from the ColorDialog.
Eg. When I choose red from ColorDialog, the combobox items' background color is red and a new items will add to the combobox, its value is the RGB value of Red color.
After I change the color to blue, the combobox items'background color is blue and a new item will add to the combobox, its value is the RGB value of blue color. So next time I could see which color I have chosen when I open the list.

The following is the code. I could change the text color but could not save it. When you choose another color. Then it all changed again. Could you help me? Thanks.
VB
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        dlgChooser.ShowDialog()
        Dim colorR As String = dlgChooser.Color.R.ToString()
        Dim colorG As String = dlgChooser.Color.G.ToString()
        Dim colorB As String = dlgChooser.Color.B.ToString()
        TextBox1.ForeColor = dlgChooser.Color()
        cmbRecent.Items.Insert(0, colorR & "," & colorG & "," & colorB)
        cmbRecent.BackColor = dlgChooser.Color()
        cmbRecent.SelectedIndex = 0
    End Sub
    Private Sub cmbRecent_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles cmbRecent.DrawItem
        If e.Index < 0 Then Exit Sub
        Dim rect As Rectangle = e.Bounds
        If e.State And DrawItemState.Selected Then
            e.Graphics.FillRectangle(SystemBrushes.Highlight, rect)
        Else : e.Graphics.FillRectangle(SystemBrushes.Window, rect)
        End If
        Dim b As New SolidBrush(Color.FromName(colorname))
        e.Graphics.FillRectangle(b, rect)
        e.Graphics.DrawRectangle(Pens.Black, rect)
        Dim b2 As SolidBrush
        Dim fillcolor As Color = Color.FromArgb(CInt(dlgChooser.Color.R), CInt(dlgChooser.Color.G), CInt(dlgChooser.Color.B))
        b2 = New SolidBrush(fillcolor)
        e.Graphics.DrawString(sender.items(e.Index), e.Font, b2, e.Bounds.X, e.Bounds.Y)
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cmbRecent.DrawMode = DrawMode.OwnerDrawVariable
    End Sub
End Class
Posted
Updated 29-Jun-10 20:46pm
v2

1 solution

Hi,

You can override the OnDrawItem of the combo box. Under this overrided method firstly fill the rectangle and then draw the rectangle using its graphics. I have done it in C#. Here is the code.

<br />
m_Color = Color.FromName((string)base.Items[e.Index]);<br />
g.FillRectangle(new SolidBrush(m_Color), e.Bounds.X + this.inMargin, e.Bounds.Y + this.inMargin, e.Bounds.Width / this.boxWidth - 2 * this.inMargin, e.Bounds.Height - 2 * this.inMargin);<br />
               <br />
g.DrawRectangle(Pens.Black, e.Bounds.X + this.inMargin, e.Bounds.Y + this.inMargin, e.Bounds.Width / this.boxWidth - 2 * this.inMargin, e.Bounds.Height - 2 * this.inMargin);<br />


Let me know in questions.

Thanks,
Bhasker
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900