Solved

Alternate rows color in a ComboBox

Posted on 2004-08-30
11
1,116 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Get size of each directory on each mapped drive 5 36
VB.Net. Reading xml value 6 29
Groupbox Control ? 2 17
VB.NET Inline If statement 4 13
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…
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

856 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