alternating the color in combobox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Salim Zaabi
    New Member
    • Oct 2009
    • 25

    #1

    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.
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    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.

    Comment

    • GaryTexmo
      Recognized Expert Top Contributor
      • Jul 2009
      • 1501

      #3
      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 OwnerDrawVariab le. 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 :)

      Code:
              private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
              {
                  Graphics g = e.Graphics;
                  ComboBox theCB = sender as ComboBox;
      
                  if (theCB != null && e.Index >= 0 && e.Index < theCB.Items.Count)
                  {
                      // Get the text
                      // Get the background colour for this istem
      
                      if ((e.State & DrawItemState.ComboBoxEdit) == DrawItemState.ComboBoxEdit)
                      {
                        // Draw an item that is showing in the combobox when not dropped down
                      }
                      else if ((e.State & DrawItemState.Selected) == DrawItemState.Selected )
                      {
                        // Draw an item that is selected
                      }
                      else
                      {
                        // Draw a normal item
                      }
                  }
              }
      (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!)

      Comment

      Working...