Results 1 to 5 of 5

Thread: listbox colors

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2004
    Posts
    8

    listbox colors

    can anybody tell me if it is possible to display items in a list box with different font colors ?

    thanks
    p_c

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Change the DrawMode property of the listbox to OwnerDrawFixed or OwnerDrawVariable and draw the items yourself.

    I believe that is the only way.
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    Like this?

    VB Code:
    1. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    2.         '    Private Sub FillListBoxWithColors()
    3.         ' Make the listbox owner-draw.
    4.         ListBox1.DrawMode = DrawMode.OwnerDrawFixed
    5.         ListBox1.ItemHeight = 24
    6.  
    7.         ' Avoid flickering.
    8.         ListBox1.BeginUpdate()
    9.         ListBox1.Items.Clear()
    10.  
    11.         ' Create a list of all the properties in the Color class.
    12.         Dim pi As Reflection.PropertyInfo
    13.         For Each pi In GetType(Color).GetProperties(Reflection.BindingFlags.Static Or Reflection.BindingFlags.Public)
    14.             ' Add the name of the property (that is, the color) to the ListBox.
    15.             ListBox1.Items.Add(pi.Name)
    16.         Next
    17.         ' Now display the result.
    18.         ListBox1.EndUpdate()
    19.     End Sub
    20.  
    21.     ' this event handler is called for each element about to be drawn
    22.  
    23.     Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
    24.         ' Get the rectangle to be drawn.
    25.         Dim rect As Rectangle = e.Bounds
    26.  
    27.         ' If this is the selected item, draw the background.
    28.         If CBool(e.State And DrawItemState.Selected) Then
    29.             ' Fill the rectable with the highlight system color.
    30.             e.Graphics.FillRectangle(SystemBrushes.Highlight, rect)
    31.         Else
    32.             ' else, fill the rectangle with the Window system color.
    33.             e.Graphics.FillRectangle(SystemBrushes.Window, rect)
    34.         End If
    35.  
    36.         ' Get the color of the item to be drawn.
    37.         Dim colorName As String = CStr(ListBox1.Items(e.Index))
    38.         ' Build a brush of that color
    39.         Dim b As New SolidBrush(Color.FromName(colorName))
    40.  
    41.         ' Shrink the rectangle by some pixels.
    42.         rect.Inflate(-16, -2)
    43.         ' Draw the rectangle interior.
    44.         e.Graphics.FillRectangle(b, rect)
    45.         ' Draw the rectable outline.
    46.         e.Graphics.DrawRectangle(Pens.Black, rect)
    47.  
    48.         ' select an appropriate color for the brush
    49.         Dim b2 As Brush
    50.         If CInt(b.Color.R) + CInt(b.Color.G) + CInt(b.Color.B) > 128 * 3 Then
    51.             b2 = Brushes.Black
    52.         Else
    53.             b2 = Brushes.White
    54.         End If
    55.  
    56.         ' draw the name of the color using the default font.
    57.         e.Graphics.DrawString(colorName, e.Font, b2, rect.X + 4, rect.Y + 2)
    58.  
    59.         ' destroy the custom brush
    60.         ' (don't dispose b2, because it is a system brush)
    61.         b.Dispose()
    62.     End Sub
    63.  
    64.     ' this is necessary, otherwise items don't redraw correctly when the form resizes.
    65.     Private Sub ListBox1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.Resize
    66.         ListBox1.Refresh()
    67.     End Sub
    68.  
    69.     ' display the RGB value of the color under the mouse cursor
    70.  
    71.     Private Sub ListBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseMove
    72.         ' don't do anything if we aren't showing colors
    73.  
    74.         If ListBox1.DrawMode = DrawMode.Normal Then Exit Sub
    75.  
    76.         ' get the index of the element under the mouse cursor.
    77.         Dim index As Integer = ListBox1.IndexFromPoint(e.X, e.Y)
    78.         ' exit if no valid entry.
    79.         If index = ListBox.NoMatches Then Exit Sub
    80.  
    81.         ' get the corresponding color.
    82.         Dim c As Color = Color.FromName(CStr(ListBox1.Items(index)))
    83.     End Sub

    Pretty, isn't it

    EDIT at the prompting of BrownMonkey

    With acknowledgements to Francesco Balena's book ISBN 0-7356-1375-3. (From which I find most of the responses I post )
    Last edited by taxes; Jul 9th, 2004 at 05:43 AM.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  4. #4
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    by Francesco Balena

  5. #5
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Originally posted by brown monkey
    by Francesco Balena
    Of course. You don't think I could have done that, do you ? I'm only just beginning to cope with his book after a year!
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width