05/August/2009
I have created a custom class with code, please check the attachments, the class will be there.
Well, drawing a ListBox isn't as hard as you might think, it's really easy. Take a look at the commented code here:
Declare a ListBox with events, so we can draw to it later...
vb.net Code:
Public WithEvents ColoredListBox As New ListBox()
Create the ListBox...
vb.net Code:
Public Sub CreateMyListBox() 'Set the properties you want the listbox to have... 'ColoredListBox.Items.Clear() ColoredListBox.Items.AddRange(New Object() {"Gray", "Green", "Yellow", "Blue", "Pink", "Cyan"}) ColoredListBox.Location = New System.Drawing.Point(12, 12) ColoredListBox.Size = New System.Drawing.Size(260, 224) 'If you set the draw mode to normal, you will end up with a normal-looking listbox _ 'with the default characteristics, however, if it's OwnerDrawFixed, means that _ 'you can "play with it" ColoredListBox.DrawMode = DrawMode.OwnerDrawFixed 'Here we add the listbox to the parent control, in this case, form1 Controls.Add(ColoredListBox) End Sub
Now, let's draw our items. Each one of these items have a different color.
vb.net Code:
Private Sub ColoredListBox_DrawItem _ (ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) _ Handles ColoredListBox.DrawItem 'Draw the background that we are later painting on e.DrawBackground() 'We set the brush default color to white Dim myBrush As Brush = Brushes.White 'This is the brush for changing the item colors.. Dim itemColor As Brush = Brushes.Black 'And the custom border color to yellow Dim myPen As Pen = Pens.Yellow 'Of course, another font... Dim myFont As New Font("Tahoma", 8) 'Since the item is always focused and selected, we have to check _ 'for both of these drawing states... If e.State = (DrawItemState.Selected Or DrawItemState.Focus) Then 'If its true then we highlight the item with a black _ 'background and a red border... myBrush = Brushes.Black myPen = Pens.Red End If 'Change the color of the item depending on it's index Select Case e.Index Case 0 itemColor = Brushes.Gray Case 1 itemColor = Brushes.Green Case 2 itemColor = Brushes.Yellow Case 3 itemColor = Brushes.Blue Case 4 itemColor = Brushes.Pink Case 5 itemColor = Brushes.Cyan End Select 'Now we draw :D 'We draw the background of each item depeding if it's focused/selected or not e.Graphics.FillRectangle(myBrush, e.Bounds) 'Here it draws the border depeding on it's state (the listbox item) e.Graphics.DrawRectangle(myPen, e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height) 'Finally we draw the string... e.Graphics.DrawString(ColoredListBox.Items(e.Index).ToString(), _ myFont, itemColor, e.Bounds.X + 2, e.Bounds.Y, StringFormat.GenericDefault) 'We added 2 to the X bound so that the string won't be touching the border.. End Sub
That's it! You're done! Easy, huh?
I've attached an example project.
Thanks to jmcilhinney, NickThissen and Empyreal for baring with me.
Please give the necessary credits if you are posting or distributing this code in another website, online service, etc. Thanks


. Each one of these items have a different color.
.
Reply With Quote


