473,396 Members | 1,826 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

ComboBox Colors

Hi,

I have the following code for combobox:

CB1.additem("One") 'Red Color
CB1.additem("two") 'Blue Color
CB1.additem("three") 'Green Color
CB1.additem("four") 'Yellow Color

I need to add different color to these items. Is there a way to do it?

Thanks

-Sonu
Nov 21 '05 #1
1 1553
Hi,

Set the comboboxes Drawmode to ownerdrawnfixed and draw the item
yourself. Here is an example.

Dim arColor() As String

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

ComboBox1.DrawMode = DrawMode.OwnerDrawFixed

ComboBox1.Location = New Point(60, 60)

ComboBox1.Width = 150

ComboBox1.Visible = True

ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList

arColor = KnownColor.GetNames(GetType(KnownColor))

ComboBox1.Items.AddRange(arColor)

Me.Text = "Ownerdrawn Combobox"

End Sub

Private Sub combobox1_DrawItem(ByVal sender As Object, ByVal e As
System.Windows.Forms.DrawItemEventArgs) Handles ComboBox1.DrawItem

Dim g As Graphics = e.Graphics

Dim s As String

Dim d As Date

Dim dr As DataRowView

Dim dt As Date

Dim br As Brush = SystemBrushes.WindowText

Dim brBack As Brush

Dim rDraw As Rectangle

Dim bSelected As Boolean = CBool(e.State And DrawItemState.Selected)

Dim bValue As Boolean = CBool(e.State And DrawItemState.ComboBoxEdit)

rDraw = e.Bounds

rDraw.Inflate(-1, -1)

If bSelected And Not bValue Then

brBack = Brushes.LightBlue

g.FillRectangle(Brushes.LightBlue, rDraw)

g.DrawRectangle(Pens.Blue, rDraw)

Else

brBack = Brushes.White

g.FillRectangle(brBack, e.Bounds)

End If

br = Nothing

brBack = Nothing

rDraw = Nothing

Try

s = ComboBox1.Items.Item(e.Index).ToString

Catch

s = ""

End Try

Dim x, y As Integer

x = e.Bounds.Left + 25

y = e.Bounds.Top + 1

Dim c As Color

Dim b As SolidBrush

c = Color.FromName(s)

b = New SolidBrush(c)

g.FillRectangle(b, x - 20, y + 2, 10, 10)

g.DrawString(s, ComboBox1.Font, Brushes.Black, x, y)

End Sub

Ken

------------------------

"Sumi" <re*********@hotmaill.com> wrote in message
news:OZ**************@TK2MSFTNGP11.phx.gbl...
Hi,

I have the following code for combobox:

CB1.additem("One") 'Red Color
CB1.additem("two") 'Blue Color
CB1.additem("three") 'Green Color
CB1.additem("four") 'Yellow Color

I need to add different color to these items. Is there a way to do it?

Thanks

-Sonu

Nov 21 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Randy | last post by:
Hello, I've got a Form which has a dataGrid on it. In one of the columns of the dataGrid, I've implemented a ComboBox using a DataGridComboBoxColumn Class. I've also over ridden the other...
1
by: TT (Tom Tempelaere) | last post by:
Hi there, I made a custom drawn ComboBox. The DrawItem event handler is (explanation follows) <code> private void OnDrawItem ( object sender, System.Windows.Forms.DrawItemEventArgs e ) {
3
by: Brian Mitchell | last post by:
Is it possible to change the fore and back colors of a combo box when its enabled property is set to false? I need to lock the value of my combo boxes (and still keep its color) but that control...
1
by: Henry Wu | last post by:
Hi guys, whenever you go to the properties window and use the ForeColor or BackColor combobox to set colors for your buttons, textbox, etc, a 3-tab dropdown list will appear tab1 is Custom, tab2...
2
by: cashdeskmac | last post by:
I am building an application and would like to give the user the chance to configure the look of the user interface. I am adding a ComboBox to a DialogBox and want to populate it with the...
3
by: Dave Wurtz | last post by:
Can anyone tell me how to get the current color of a combobox's button? I have a custom control that I want to look similar to a combobox. It contains a button control that I want to look like the...
5
by: Steve B. | last post by:
Without adding whitespace to the ComboBox datasource is there a way I can add a blank entry or, a reset entry, to the ComboBox dropdown Thanks Steve
1
by: jesuran | last post by:
Hi, I have got a combobox which retrieves its data from a dataset: Code: Private dvsItem As DataView dvsItem = New DataView(MainForm.TempPrice.TempPriceBook.Copy) dvsItem.Sort = "ItemCode"...
4
by: Lars E. Nes | last post by:
Hi all. I am setting some color on my combo's: this.combobox1.BackColor = Colors.White; This only works when the combobox is enabled. How can i add color to a disabled combobox? Lars
3
by: =?Utf-8?B?R2lkaQ==?= | last post by:
Hi, In my form, I want to have a comboBox fo colors. on each color i want to add my remark (like in outlook calendar), how can i do it? Thanks, Gidi.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.