none
How to change background color of combobox items at run time?

    Question

  • I am developing a Quiz project(Multiple choice questions) in VB.Net . I have a combo box in my project which contains list of question numbers(example 1,2,3.....) and if the user clicks on the question number it will take to it's corresponding question with it's equivalent options.

    I want the combo box items back color or fore color to be changed once if it's answer is selected. For example the when the combo box is clicked then it should display all the question numbers and should depict the questions which have been answered and also the questions which are not answered through BACK COLOR OR FORE COLOR.

    Please can anyone give me a sample code to do this task.

    Thank you in advance

    Tuesday, March 21, 2017 1:11 PM

Answers

  • Here is a way to do that

    Option Strict On
    
    Public Class Form1
    
        Private cb As New CCombobox With {.Parent = Me, .Location = New Point(10, 10)}
    
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            cb.Items.Add(New CComboboxItem("Item Number 1", Color.Green, Color.Yellow))
            cb.Items.Add(New CComboboxItem("Item Number 2", Color.Blue, Color.Red))
            cb.Items.Add(New CComboboxItem("Item Number 3", Color.Red, Color.Plum))
        End Sub
    End Class
    
    
    
    
    
    Class CCombobox : Inherits ComboBox
    
        Sub New()
            Me.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed
        End Sub
    
        Protected Overrides Sub OnDrawItem(e As DrawItemEventArgs)
            MyBase.OnDrawItem(e)
            Dim item As CComboboxItem = DirectCast(Me.Items(e.Index), CComboboxItem)
            Using ForeBrush As Brush = New SolidBrush(item.Forecolor)
                Using BackBrush As Brush = New SolidBrush(item.Backcolor)
                    e.Graphics.FillRectangle(BackBrush, e.Bounds)
                    e.Graphics.TranslateTransform(0, e.Bounds.Y)
                    e.Graphics.DrawString(item.ToString, Me.Font, ForeBrush, 0, 0)
                End Using
            End Using
        End Sub
    End Class
    
    
    
    
    
    Class CComboboxItem
     
        Public Sub New(Value As Object, Color As Color, BackColor As Color)
            Me.Value = Value
            Me.Forecolor = Color
            Me.Backcolor = BackColor
        End Sub
    
        Public Property Value As Object = Nothing
        Public Property Forecolor As Color = Color.Black
        Public Property Backcolor As Color = Color.Black
    
        Public Overrides Function ToString() As String
            Return Value.ToString
        End Function
    
    End Class


    Luc


    • Marked as answer by Tabzee Tuesday, March 21, 2017 7:51 PM
    • Edited by Crazypennie Wednesday, March 22, 2017 12:42 AM
    Tuesday, March 21, 2017 7:47 PM

All replies

  • Here is a way to do that

    Option Strict On
    
    Public Class Form1
    
        Private cb As New CCombobox With {.Parent = Me, .Location = New Point(10, 10)}
    
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            cb.Items.Add(New CComboboxItem("Item Number 1", Color.Green, Color.Yellow))
            cb.Items.Add(New CComboboxItem("Item Number 2", Color.Blue, Color.Red))
            cb.Items.Add(New CComboboxItem("Item Number 3", Color.Red, Color.Plum))
        End Sub
    End Class
    
    
    
    
    
    Class CCombobox : Inherits ComboBox
    
        Sub New()
            Me.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed
        End Sub
    
        Protected Overrides Sub OnDrawItem(e As DrawItemEventArgs)
            MyBase.OnDrawItem(e)
            Dim item As CComboboxItem = DirectCast(Me.Items(e.Index), CComboboxItem)
            Using ForeBrush As Brush = New SolidBrush(item.Forecolor)
                Using BackBrush As Brush = New SolidBrush(item.Backcolor)
                    e.Graphics.FillRectangle(BackBrush, e.Bounds)
                    e.Graphics.TranslateTransform(0, e.Bounds.Y)
                    e.Graphics.DrawString(item.ToString, Me.Font, ForeBrush, 0, 0)
                End Using
            End Using
        End Sub
    End Class
    
    
    
    
    
    Class CComboboxItem
     
        Public Sub New(Value As Object, Color As Color, BackColor As Color)
            Me.Value = Value
            Me.Forecolor = Color
            Me.Backcolor = BackColor
        End Sub
    
        Public Property Value As Object = Nothing
        Public Property Forecolor As Color = Color.Black
        Public Property Backcolor As Color = Color.Black
    
        Public Overrides Function ToString() As String
            Return Value.ToString
        End Function
    
    End Class


    Luc


    • Marked as answer by Tabzee Tuesday, March 21, 2017 7:51 PM
    • Edited by Crazypennie Wednesday, March 22, 2017 12:42 AM
    Tuesday, March 21, 2017 7:47 PM
  • Thank you
    Tuesday, March 21, 2017 7:50 PM