473,320 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

alternating the color in combobox

Hi Everybody,
Anyone have an idea on alternating the color of each line in combobox. Example, white and gray. I saw an example but it was for web application and I'm looking for example to use in windows application.
Mar 14 '10 #1
2 6884
GaryTexmo
1,501 Expert 1GB
Hmm, I did a good bit of googling around and I couldn't find anything on this... I'm not sure if it's possible, at least from a built into .NET perspective. I could only find examples for web forms as well. Maybe someone else might know a way to do it with the standard .NET combo box object.

You could try looking into creating your own combo box object. I did some googling and found this, it should help you out.

http://www.codeproject.com/KB/combob...tComboBox.aspx
Mar 15 '10 #2
GaryTexmo
1,501 Expert 1GB
You, sir, are in luck!

It just so happened that I visited a coworker's office just as he was looking at a page that I had seen earlier this morning while trying to research a way to help you. We got to talking and it turns out what you want to do is quite possible!

On a ComboBox, there is a property called DrawMode. You can change this so that you can handle the way a combo box item is drawn. There are two modes in addition to Normal, one is OwnerDrawFixed and the other is OwnerDrawVariable. These have to do with the width of the combo box pane... you can look these up on MSDN for more information.

Suffice it to say, if the DrawMode is set to one of these, drawing is now handled by the DrawItem event, which you can attach an event handler to and draw whichever way you like. Here's a rough breakdown of what you'd want to do... I'll leave it to you to learn the details though :)

Expand|Select|Wrap|Line Numbers
  1.         private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
  2.         {
  3.             Graphics g = e.Graphics;
  4.             ComboBox theCB = sender as ComboBox;
  5.  
  6.             if (theCB != null && e.Index >= 0 && e.Index < theCB.Items.Count)
  7.             {
  8.                 // Get the text
  9.                 // Get the background colour for this istem
  10.  
  11.                 if ((e.State & DrawItemState.ComboBoxEdit) == DrawItemState.ComboBoxEdit)
  12.                 {
  13.                   // Draw an item that is showing in the combobox when not dropped down
  14.                 }
  15.                 else if ((e.State & DrawItemState.Selected) == DrawItemState.Selected )
  16.                 {
  17.                   // Draw an item that is selected
  18.                 }
  19.                 else
  20.                 {
  21.                   // Draw a normal item
  22.                 }
  23.             }
  24.         }
(Note, all credit for this slick little solution doesn't belong to me :D I was just a crazy fluke that my coworker just happened to be working on a similar problem. It was a great chance for me to learn something new about .NET though!)
Mar 15 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: bettyann | last post by:
i am using templates via HTML_Template_IT to create a table from the results of a mysql query. i wanted to change the background image of alternating rows in the table. i thought i could do this...
1
by: Alistair Birch | last post by:
Hi I want rows of a table to appear in alternating background colours. Having looked around the web I can't find any solution apart from waiting for the next version of CSS, so I tried building...
47
by: Matt Kruse | last post by:
http://www.mattkruse.com/temp/css_expressions.html One of the standard CSS questions is "how can I shade every other table row a different color with CSS?" The answers are usually 1) you can't...
2
by: Savvas | last post by:
Hi I am trying to make my datagrid look a bit better than the typical one. How do i show the datagrid to have alternating colours e.g. like in windows media player v.10? Thanks Savvas
1
by: Eirik Eldorsen | last post by:
I'm trying to set alternating bgcolor for a datalist with 2 columns. My problem is that its the alternating cell that get the bgcolor, not the row. Is it possible to set alternating color of rows?...
9
by: Max Weebler | last post by:
Hi, I have a datagrid built that has an alternating item style that sets the backcolor and ForeColor of its rows. I have 4 template columns. One of them has a LinkButton embedded in it to...
1
by: rockdale | last post by:
Hi, All I have an datagrid with alternating color, I also want the back-color of my textbox in ItemTemplate also appears alternating, how could I do that? <asp:datagrid id="dgridEmpHours"...
3
by: Daniel Manes | last post by:
My DataGridView is set up like this: * Main row background color = white * Alternating row background color = light gray I also have a read-only (non-editable) column I want to show up as solid...
4
by: mike | last post by:
how can I change the font color for an alternating row where the column data is formatted as a link? setting a style in the stylesheet for a { color:white; }
1
by: Sachin | last post by:
Hi All, I am using ASP.NET Repeater Web Control. I want to use different stylesheets for alternating rows similar to DataGrid contorl. How can I do this? Thanks In Advance Sachin
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.