Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Color combobox
Message
 
To
10/06/2004 13:38:52
Mike Cole
Yellow Lab Technologies
Stanley, Iowa, United States
General information
Forum:
ASP.NET
Category:
Other
Title:
Miscellaneous
Thread ID:
00912331
Message ID:
00913350
Views:
12
Hi Mike,

I just presented how to do this at the last conference I spoke at. Here is the information and code:

You need to set the combobox/listbox DrawMode property to "OwnerDrawFixed"

Then in the DrawItem event handler for the combobox/listbox, include the following. A rectangle will be drawn inside each item the color of the item to draw. This code assumes that the combobox/listbox is bound to an array that contains the colors to be displayed.
' Array of colors the combobox/listbox is bound to
Private RainbowColors As Color() =  {Color.Red, Color.Orange, Color.Yellow, Color.Green, Color.Blue, Color.Indigo, Color.Violet}

Private Sub lstColors_DrawItem(sender As Object, e As System.Windows.Forms.DrawItemEventArgs) Handles lstColors.DrawItem
   ' Use White text for selected items
   '  and black for the others.
   Dim brText As Brush
   If(e.State And DrawItemState.Selected) = DrawItemState.Selected Then
      brText = Brushes.White
   Else
      brText = Brushes.Black
   End If
   
   ' Have Windows draw the background.
   e.DrawBackground()
   
   ' Create a 15 by 10 pixel rectangle,
   '  slightly offset from the list item.
   Dim rct As New Rectangle(e.Bounds.X + 5, e.Bounds.Y + 5, 15, 10)
   
   ' Create a brush of the correct color
   '  to fill in a rectangle to the
   '  left of the text.
   ' Draw and fill the rectangle for the color.
   e.Graphics.DrawRectangle(Pens.Black, rct)
   Dim brColor As New SolidBrush(RainbowColors(e.Index))
   Try
      e.Graphics.FillRectangle(brColor, rct)
   Finally
      brColor.Dispose()
   End Try 
   ' Draw the text next to the rectangle.
   e.Graphics.DrawString(RainbowColors(e.Index).Name, lstColors.Font, brText, rct.Left + rct.Width + 5, e.Bounds.Y)
   
   ' Have Windows draw the dotted focus rectangle.
   e.DrawFocusRectangle()
End Sub 
>Hello,
>In my app, I would like to give the user ability to select a certain color from a combo box. I don't just want to make a plain jane combobox that has the names of the colors in it, but I want to make one that actually has splashes of color in it. Can anybody point me in the correct direction?
-----------------------------------------

Cathi Gero, CPA
Prenia Software & Consulting Services
Microsoft C# / .NET MVP
Mere Mortals for .NET MVP
cgero@prenia.com
www.prenia.com
Weblog: blogs.prenia.com/cathi
Previous
Reply
Map
View

Click here to load this message in the networking platform