?
Solved

Alternate rows color in a ComboBox

Posted on 2004-08-30
11
Medium Priority
?
1,175 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 2000 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month15 days, 15 hours left to enroll

741 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