Ghanisen
asked on
Alternate rows color in a ComboBox
Hi,
Does anyone know how to assign a different color (e.g. AliceBue) to alternate rows in a ComboBox?
Thanks
Does anyone know how to assign a different color (e.g. AliceBue) to alternate rows in a ComboBox?
Thanks
ASKER
Hi Ronald,
Where do I call the ComBox1_DrawnItem from?
Thanks for your 2 prompt responses which I'm now testing.
Where do I call the ComBox1_DrawnItem from?
Thanks for your 2 prompt responses which I'm now testing.
Drawitem is a standard event of a combobox you don't have to call it, as you can see in the sub
Private Sub ComboBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawI temEventAr gs) Handles ComboBox1.DrawItem <------
Private Sub ComboBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawI
ASKER
Hi Ronald,
I pasted your code and changed ComboBox1 to Combo1 (Which is the name of my ComboBox).
No exception is raised but nothing happens. All lines of the ComboBox remain white.
Can you please suggest something Ronald?
Thanks
I pasted your code and changed ComboBox1 to Combo1 (Which is the name of my ComboBox).
No exception is raised but nothing happens. All lines of the ComboBox remain white.
Can you please suggest something Ronald?
Thanks
ASKER
Hi Ronald,
Sorry, I had forgotten to set the drawmode to ownerdrawnfixed or ownerdrawnvariable!
Now I did but I get the following error : Invalid cast of the 'DataRowView' type in 'String' type
The error is provoked by the last line of your code:
e.Graphics.DrawString(Me.C ombo1.Item s.Item(e.I ndex), Me.Combo1.Font, New SolidBrush(Color.Black), New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height)).
We're real close because I can see the red colour in the ComboBox when the form is trying to load.
Do you see what's wrong?
Thanks Ronald.
Sorry, I had forgotten to set the drawmode to ownerdrawnfixed or ownerdrawnvariable!
Now I did but I get the following error : Invalid cast of the 'DataRowView' type in 'String' type
The error is provoked by the last line of your code:
e.Graphics.DrawString(Me.C
We're real close because I can see the red colour in the ComboBox when the form is trying to load.
Do you see what's wrong?
Thanks Ronald.
Hi Ghasinen,
I'll have A look at it
I'll have A look at it
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi Ronald,
It works!. You're real smart because that was a tough one!
You sure deserves the 500 points and I wish I could make them 5000!
Many thanks.
It works!. You're real smart because that was a tough one!
You sure deserves the 500 points and I wish I could make them 5000!
Many thanks.
Something else you may want to consider is the Infragistics NetAdvantage 2004 Suite. This suite of controls offers VERY rich interface options. In this case all you would have had to do was set an Alternate Row Color property!
Check them out at http://www.infragistics.com. Cost starts at $499 and goes up from there based on support options chosen.
Check them out at http://www.infragistics.com. Cost starts at $499 and goes up from there based on support options chosen.
ASKER
Hi Ronalds,
I have ComponentOne StudioEntreprise with all the controls and other tools
but I'm new to programming and I want to learn the stuff. I'll use ready made components after I get sufficient knowledge of VB.NET and thanks to guys like you and the bunch of expert-echange I'm making very rapid progress.
Thanks anyway.
I have ComponentOne StudioEntreprise with all the controls and other tools
but I'm new to programming and I want to learn the stuff. I'll use ready made components after I get sufficient knowledge of VB.NET and thanks to guys like you and the bunch of expert-echange I'm making very rapid progress.
Thanks anyway.
ASKER
Sorry ptakja. The comment was for you. Thanks a lot for the suggestion.
set the drawmode to ownerdrawnfixed or ownerdrawnvariable and add this sub
Private Sub ComboBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawI
Dim c As System.Drawing.Color
If e.Index < 0 Then
e.DrawBackground()
e.DrawFocusRectangle()
Exit Sub
End If
'Determine colour to use
If e.Index Mod 2 = 0 Then
c = Color.Red
Else
c = Color.Green
End If
e.DrawBackground()
e.DrawFocusRectangle()
e.Graphics.DrawRectangle(N
e.Graphics.FillRectangle(N
e.Graphics.DrawString(Me.C
End Sub