|
-
Apr 25th, 2013, 07:42 AM
#1
Thread Starter
Addicted Member
[RESOLVED] ComboBox with different color Items and DropDownList Style
Hello everybody,
I want to have a combobox that has different colors for it's items. So here is an example. New Form with an added combobox.
'STRUCTURE THAT HOLDS THE DATA FOR THE COMBOBOX ITEMS
Public Structure ColoredComboboxItem
Dim Text As String
Dim Color As Color
Public Overrides Function ToString() As String
Return Text
End Function
End Structure
WHEN FORM LOADS, LOAD SOME DATA INTO THE COMBOBOX:
Dim myItem As New ColoredComboboxItem
With myItem
.Text = "Festo AG"
.Color = Color.Black
End With
ComboBox1.Items.Add(myItem)
myItem = New ColoredComboboxItem
With myItem
.Text = "Bosch-Rexroth"
.Color = Color.LightGray
End With
ComboBox1.Items.Add(myItem)
myItem = New ColoredComboboxItem
With myItem
.Text = "Parcon"
.Color = Color.Black
End With
ComboBox1.Items.Add(myItem)
and the last thing is to write the DrawItem event for the combobox:
Private Sub ComboBox1_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ComboBox1.DrawItem
e.DrawBackground()
'GET ITEM TO DRAW
Dim myItem As ColoredComboboxItem = DirectCast(ComboBox1.Items(e.Index), ColoredComboboxItem)
'DRAW TEXT USING SPECIFIED FONT AND COLOR
If e.State = 769 Then
e.Graphics.DrawString(myItem.Text, e.Font, New SolidBrush(Color.White), e.Bounds)
Else
e.Graphics.DrawString(myItem.Text, e.Font, New SolidBrush(myItem.Color), e.Bounds)
End If
e.DrawFocusRectangle()
End Sub
The combo must have the DrawItem property set to OwnerDrawFixed in order to work.
NOW, this works only if the DropDownStyle property is set to DropDown but i need it to be DropDownList so the user can't change any of the items it contains.
If I do it like this I get: InvalidArgument=Value of '-1' is not valid for 'index'. at the DIM line in the Draw item event.
I tried to insert If e.index<>-1 then in the drawItem event and it works. But then the e.DrawBackground() doesn't work the way it should. Background is blue but text must be white or lightgray in order to have a contrast.
Any help with this?
Thx
Last edited by ovi_gm; Apr 25th, 2013 at 07:48 AM.
-
Apr 25th, 2013, 11:17 AM
#2
Re: ComboBox with different color Items and DropDownList Style
Not sure where you got "If e.State = 769" from, but I do it like this and seems to work as expected...
Code:
'DRAW TEXT USING SPECIFIED FONT AND COLOR
If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then
e.Graphics.DrawString(myItem.Text, e.Font, New SolidBrush(Color.White), e.Bounds)
Else
e.Graphics.DrawString(myItem.Text, e.Font, New SolidBrush(myItem.Color), e.Bounds)
End If
-
Apr 25th, 2013, 11:54 AM
#3
Thread Starter
Addicted Member
Re: ComboBox with different color Items and DropDownList Style
Well, as far as I understood from Dunfiddlin here on the forum the 769 is the equivalent for DrawItemState.Selected.
Something happens in VB when I also put a label or a button on the form and the DraItem event doesn't work anymore.
See this post:
http://www.vbforums.com/showthread.p...election-color
Now I cheked your code and it works if the DropDownStyle property of the combo is set to DropDown. But it still doesn't work with DropDownList and this is what I needed.
-
Apr 25th, 2013, 04:17 PM
#4
Re: ComboBox with different color Items and DropDownList Style
It's all in those e.State values!
vb.net Code:
Private Sub ComboBox1_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ComboBox1.DrawItem
e.DrawBackground()
'GET ITEM TO DRAW
If Not e.Index = -1 Then
Dim myItem As ColoredComboboxItem = DirectCast(ComboBox1.Items(e.Index), ColoredComboboxItem)
'DRAW TEXT USING SPECIFIED FONT AND COLOR
ListBox1.Items.Add(CInt(e.State).ToString)
If e.State = 769 Or e.State = 785 Or e.State = 4881 Then
e.Graphics.DrawString(myItem.Text, e.Font, New SolidBrush(Color.White), e.Bounds)
Else
e.Graphics.DrawString(myItem.Text, e.Font, New SolidBrush(myItem.Color), e.Bounds)
End If
End If
e.DrawFocusRectangle()
End Sub
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Apr 25th, 2013, 04:40 PM
#5
Re: ComboBox with different color Items and DropDownList Style
 Originally Posted by ovi_gm
Now I cheked your code and it works if the DropDownStyle property of the combo is set to DropDown. But it still doesn't work with DropDownList and this is what I needed.
Well I have the combo set to OwnerDrawFixed and DropDownList, works just fine, you must have some sort of other problems, And adding buttons or labels to a form shouldn't have any effect on the way an owner drawn control draws, at least I've never seen it have any! BTW, Win7 with themes enabled here.
Edit: "If e.State = 769 Or e.State = 785 Or e.State = 4881 Then"... acts the same way as what I posted above, I don't see any difference. 
Good Luck!
-
Apr 25th, 2013, 05:00 PM
#6
Re: ComboBox with different color Items and DropDownList Style
And adding buttons or labels to a form shouldn't have any effect on the way an owner drawn control draws
It doesn't. It has an effect on the way that e.State is reported. Without a label or a button on the form you get a single state reported. With a label or a button on the form, it reports combinations of states. I have absolutely no idea why that's true but trust me, after a fun hour or two tracking every change, there is no doubt that it is.
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Apr 26th, 2013, 01:00 AM
#7
Thread Starter
Addicted Member
Re: ComboBox with different color Items and DropDownList Style
Hello and thank you for your answers.
Edgemeal, at home I also have Win7 and it works ok. But in windows xp it is not working ok. The text of the selection remains black and it is very hard to read over a blue background.
Anyway, Dunfiddlin's code works ok for me now.
Topic resolved.
Thx a lot.
-
Apr 26th, 2013, 01:06 AM
#8
Re: ComboBox with different color Items and DropDownList Style
 Originally Posted by dunfiddlin
It's all in those e.State values!
vb.net Code:
Private Sub ComboBox1_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ComboBox1.DrawItem e.DrawBackground() 'GET ITEM TO DRAW If Not e.Index = -1 Then Dim myItem As ColoredComboboxItem = DirectCast(ComboBox1.Items(e.Index), ColoredComboboxItem) 'DRAW TEXT USING SPECIFIED FONT AND COLOR ListBox1.Items.Add(CInt(e.State).ToString) If e.State = 769 Or e.State = 785 Or e.State = 4881 Then e.Graphics.DrawString(myItem.Text, e.Font, New SolidBrush(Color.White), e.Bounds) Else e.Graphics.DrawString(myItem.Text, e.Font, New SolidBrush(myItem.Color), e.Bounds) End If End If e.DrawFocusRectangle() End Sub
You should absolutely never be comparing a variable or property whose type is an Enum with numbers like that. Absolutely NEVER. As Edgemeal has shown, if you're interested in one specific value then you can pick that value out of an arbitrary combination regardless of what the other values are with a bitwise logic. If you're interested in specific combinations then you should specify those combinations as their Enum values, not as numbers. To combine two Enum values you use the bitwise Or operator. Or is to combine, And is to test, Xor is to toggle, And Not is to remove.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|