Link to home
Start Free TrialLog in
Avatar of Ghanisen
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
Avatar of RonaldBiemans
RonaldBiemans

try  this

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.DrawItemEventArgs) Handles ComboBox1.DrawItem

        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(New Pen(c), New Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))
        e.Graphics.FillRectangle(New SolidBrush(c), New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))
        e.Graphics.DrawString(Me.ComboBox1.Items.Item(e.Index), Me.ComboBox1.Font, New SolidBrush(Color.Black), New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))

    End Sub
Avatar of Ghanisen

ASKER

Hi Ronald,

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.DrawItemEventArgs) Handles ComboBox1.DrawItem  <------

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

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.Combo1.Items.Item(e.Index), 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.
Hi Ghasinen,

I'll have A look at it
ASKER CERTIFIED SOLUTION
Avatar of RonaldBiemans
RonaldBiemans

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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.
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.
Sorry ptakja. The comment was for you. Thanks a lot for the suggestion.