Solved

Alternate rows color in a ComboBox

Posted on 2004-08-30
11
1,085 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
  • 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
 

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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now