Solved

Alternate rows color in a ComboBox

Posted on 2004-08-30
11
1,126 Views
Last Modified: 2012-08-13
Hi,

Does anyone know how to assign a different color (e.g. AliceBue) to alternate rows in a ComboBox?

Thanks
0
Comment
Question by:Ghanisen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
11 Comments
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 11929394
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
0
 

Author Comment

by:Ghanisen
ID: 11929574
Hi Ronald,

Where do I call the ComBox1_DrawnItem from?

Thanks for your 2 prompt responses which I'm now testing.
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 11929689
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  <------

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Ghanisen
ID: 11930030
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

0
 

Author Comment

by:Ghanisen
ID: 11930244
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.
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 11930283
Hi Ghasinen,

I'll have A look at it
0
 
LVL 25

Accepted Solution

by:
RonaldBiemans earned 500 total points
ID: 11930312
Hi Ghasinen,

change to someting like

        e.Graphics.DrawString(CType(Me.ComboBox1.Items.Item(e.Index), DataRowView).Item("yourdisplaycolumn"), Me.ComboBox1.Font, New SolidBrush(Color.Black), New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))
0
 

Author Comment

by:Ghanisen
ID: 11930366
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.
0
 
LVL 14

Expert Comment

by:ptakja
ID: 11931535
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.
0
 

Author Comment

by:Ghanisen
ID: 11931599
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.
0
 

Author Comment

by:Ghanisen
ID: 11931651
Sorry ptakja. The comment was for you. Thanks a lot for the suggestion.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Generate Unique ID in VB.NET 21 101
Get sourcecode path 14 46
Need help with a query 3 37
Visual Basic Json to CSV Converter - Converting large json files to csv 2 50
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question