Results 1 to 19 of 19

Thread: Each Listbox item with a different colour

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 1999
    Location
    Liskeard, cornwall
    Posts
    21

    Post

    I would like to add an item to a listbox and give it a different colour to another item in the same listbox. I shall accept all help graciously.

    Thanx

  2. #2
    Addicted Member
    Join Date
    Jan 1999
    Posts
    239

    Post

    It can't be done. The ListBox Foreground Property sets the color of the Text for the
    complete ListBox.

    If you wanted just one line of data to have
    a different color you could make three ListBoxes with the top and bottom red and the middle blue. Then you would show the line that you wanted to highlight in the middle box, however this takes quite a bit of coding

    ------------------

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Nov 1999
    Location
    Liskeard, cornwall
    Posts
    21

    Post

    Surely there has to be a way to do it.
    There is so many other spectacular things than can be achieved using VB, yet you cant have a list of items of different colours.

    M.

  4. #4
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Post

    Sure it can be done, just not easily, here's the code I created and posted a while back, it adds the Items to the Listbox with Different Colored Backgrounds, but can be easily modified to have different colored ForeColors instead:

    Add a Listbox to a Form with the Style set to 1 - Checkbox

    In a Module..
    Code:
    Option Explicit
    
    Public Type RECT
            Left As Long
            Top As Long
            Right As Long
            Bottom As Long
    End Type
    
    Public Type DRAWITEMSTRUCT
            CtlType As Long
            CtlID As Long
            itemID As Long
            itemAction As Long
            itemState As Long
            hwndItem As Long
            hdc As Long
            rcItem As RECT
            itemData As Long
    End Type
    
    Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
    
    Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Public Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Long) As Long
    Public Declare Function FillRect Lib "user32" (ByVal hdc As Long, lpRect As RECT, ByVal hBrush As Long) As Long
    Public Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
    Public Declare Function SetBkColor Lib "gdi32" (ByVal hdc As Long, ByVal crColor As Long) As Long
    Public Declare Function SetTextColor Lib "gdi32" (ByVal hdc As Long, ByVal crColor As Long) As Long
    Public Declare Function TextOut Lib "gdi32" Alias "TextOutA" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal lpString As String, ByVal nCount As Long) As Long
    Public Declare Function DrawFocusRect Lib "user32" (ByVal hdc As Long, lpRect As RECT) As Long
    Public Declare Function GetSysColor Lib "user32" (ByVal nIndex As Long) As Long
    
    Public Const COLOR_HIGHLIGHT = 13
    Public Const COLOR_HIGHLIGHTTEXT = 14
    Public Const COLOR_WINDOW = 5
    Public Const COLOR_WINDOWTEXT = 8
    Public Const LB_GETTEXT = &H189
    Public Const WM_DRAWITEM = &H2B
    Public Const GWL_WNDPROC = (-4)
    Public Const ODS_FOCUS = &H10
    Public Const ODT_LISTBOX = 2
    
    Public lPrevWndProc As Long
    
    Public Function SubClassedList(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
        Dim tItem As DRAWITEMSTRUCT
        Dim sBuff As String * 255
        Dim sItem As String
        Dim lBack As Long
        
        If Msg = WM_DRAWITEM Then
            'Redraw the listbox
            'This function only passes the Address of the DrawItem Structure, so we need to
            'use the CopyMemory API to Get a Copy into the Variable we setup:
            Call CopyMemory(tItem, ByVal lParam, Len(tItem))
            'Make sure we're dealing with a Listbox
            If tItem.CtlType = ODT_LISTBOX Then
                'Get the Item Text
                Call SendMessage(tItem.hwndItem, LB_GETTEXT, tItem.itemID, ByVal sBuff)
                sItem = Left(sBuff, InStr(sBuff, Chr(0)) - 1)
                If (tItem.itemState And ODS_FOCUS) Then
                    'Item has Focus, Highlight it, I'm using the Default Focus
                    'Colors for this example.
                    lBack = CreateSolidBrush(GetSysColor(COLOR_HIGHLIGHT))
                    Call FillRect(tItem.hdc, tItem.rcItem, lBack)
                    Call SetBkColor(tItem.hdc, GetSysColor(COLOR_HIGHLIGHT))
                    Call SetTextColor(tItem.hdc, GetSysColor(COLOR_HIGHLIGHTTEXT))
                    TextOut tItem.hdc, tItem.rcItem.Left, tItem.rcItem.Top, ByVal sItem, Len(sItem)
                    DrawFocusRect tItem.hdc, tItem.rcItem
                Else
                    'Item Doesn't Have Focus, Draw it's Colored Background
                    'Create a Brush using the Color we stored in ItemData
                    lBack = CreateSolidBrush(tItem.itemData)
                    'Paint the Item Area
                    Call FillRect(tItem.hdc, tItem.rcItem, lBack)
                    'Set the Text Colors
                    Call SetBkColor(tItem.hdc, tItem.itemData)
                    Call SetTextColor(tItem.hdc, IIf(tItem.itemData = vbBlack, vbWhite, vbBlack))
                    'Display the Item Text
                    TextOut tItem.hdc, tItem.rcItem.Left, tItem.rcItem.Top, ByVal sItem, Len(sItem)
                End If
                Call DeleteObject(lBack)
                'Don't Need to Pass a Value on as we've just handled the Message ourselves
                SubClassedList = 0
                Exit Function
                        
            End If
                
        End If
        SubClassedList = CallWindowProc(lPrevWndProc, hwnd, Msg, wParam, lParam)
    End Function
    In the Form..
    Code:
    Private Sub Form_Load()
        Dim I As Integer
        For I = 0 To 15
            'Load a List of 0 to 15 with the Item Data
            'Set to the QBColors 0 - 15
            List1.AddItem "Color " & I
            List1.itemData(List1.NewIndex) = QBColor(I)
        Next
        'Subclass the "Form", to Capture the Listbox Notification Messages
        lPrevWndProc = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf SubClassedList)
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
        'Release the SubClassing, Very Import to Prevent Crashing!
        Call SetWindowLong(hwnd, GWL_WNDPROC, lPrevWndProc)
    End Sub

    ------------------
    Aaron Young
    Analyst Programmer
    [email protected]
    [email protected]

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Nov 1999
    Location
    Liskeard, cornwall
    Posts
    21

    Post

    Cheers Aaron ... Works great!

  6. #6
    Addicted Member Cimperiali's Avatar
    Join Date
    Oct 2002
    Location
    Milan, Italy, Europe
    Posts
    188

    Nice to read from Aaron!

    Hi, man!
    Happy to read from you again!

    Cesare Imperiali
    (ex Codeguru Rater)
    Special thanks to some wonderful people,
    such as Lothar the Great Haensler, Aaron Young,
    dr_Michael, Chris Eastwood, TheOnlyOne ClearCode....

  7. #7
    New Member
    Join Date
    Oct 2004
    Posts
    15

    thanx and also one prob

    dear friends,


    actully i want toset diffrent back color of each items of a listbox.

    i have tried "Aaron Young "'s code , its working but the style property which we set to "Checkbox" at design time ,though the checkboxes were not displyed when we run this code.


    so can u suggest me to wht should i do if iwant to set diff back /fore color of each item and also want to checkboxes of each item?


    vishal patel

  8. #8
    Hyperactive Member toughcoder's Avatar
    Join Date
    Nov 2002
    Location
    Near, Very Near
    Posts
    340
    3 cheers for Aaron. Gr8 code buddy
    If Not VB Then Exit
    ------------------------------------------------
    visit me @ http://mzubair.50g.com/

  9. #9
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091

    Re: thanx and also one prob

    Originally posted by vishalpatel
    dear friends,


    actully i want toset diffrent back color of each items of a listbox.

    i have tried "Aaron Young "'s code , its working but the style property which we set to "Checkbox" at design time ,though the checkboxes were not displyed when we run this code.


    so can u suggest me to wht should i do if iwant to set diff back /fore color of each item and also want to checkboxes of each item?


    vishal patel
    The listbox is set to owner drawn when it displays checkboxes. You are overriding the default code that draws each listitem, so you'll have to draw the checkboxes yourself as well.
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  10. #10
    New Member
    Join Date
    Oct 2004
    Posts
    15

    is there any other method to satisfy my requirement?

    tell me first how can i draw checkbox manually?

    do u meto say using putting checkboxcontrol over listbox?

    plz explain me again.

    or giv me code of doing so


    vishal

  11. #11
    Hyperactive Member toughcoder's Avatar
    Join Date
    Nov 2002
    Location
    Near, Very Near
    Posts
    340
    Hello vishal,
    u shud draw the checkbox using the API calls like Line, Rectangle. I mean draw the basic checkbox look like a windows control. Search around on www.planet-source-code.com for some examples.
    If Not VB Then Exit
    ------------------------------------------------
    visit me @ http://mzubair.50g.com/

  12. #12
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    The DrawFrameControl will do it as well.

    http://msdn.microsoft.com/library/de...tdraw_4b3g.asp
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  13. #13
    Fanatic Member Bombdrop's Avatar
    Join Date
    Apr 2001
    Location
    St Helens, England, UK
    Posts
    667
    You could also have a look at the following FREE activeX control MBListEX Control Yhis allows tou to have different fonts for each list item change back colour for each item and also to add pic to each iteam pluse helps with search of the Listbox all in in a great control

    Hope this helps

  14. #14
    New Member
    Join Date
    Oct 2004
    Posts
    15
    thanx Bombdrop,


    but as i have mention previously. irequired the listbox with checkbox and the diffrent back color for each item.


    as u have told the control "MBListEX Control " do the diffrent back color for each item but not have any property to diplay item with checkbox .



    so do u have any other suggetion.

    if , then plz help me.

    waiting for ur reply and agin thax.


    vishal

  15. #15
    New Member
    Join Date
    Jan 2012
    Posts
    6

    Re: Each Listbox item with a different colour

    I have implemented the code to set the listbox items to different colors and it works great. My issue is that I want the user to be able to multi-select in the listbox and they can not do that if the style is set to have checkboxes. Is there any way I can work around this?

  16. #16
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Each Listbox item with a different colour

    Don't add to an old thread 10 years old. Instead, start a new thread and put a link to this one if you want to add a reference to it.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  17. #17
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: Each Listbox item with a different colour

    Even older than that. Apparently someone revived it about 10 years ago but the thread was started in 1999 !

  18. #18
    Frenzied Member
    Join Date
    May 2014
    Location
    Kallithea Attikis, Greece
    Posts
    1,309

    Re: Each Listbox item with a different colour

    Maybe MKaufman you need a glist control...
    Look here http://www.vbforums.com/showthread.p...rius-Selectors

    The glist4 control have events for item drawing (if you want it). There are some selectors and one is for color picking. That selector displays 16Milion items with the right background...exposing the rectangle for drawing and using a "bypass" way to setup a fake list (the list of 16Milions...exist only for the user, not internally).
    The code of Aaron Young is very good..but uses the same old listbox. The new listbox has no dependencies for common controls, also the scroll bar is internally drawing..using three shapes. Is unicode capable (need unicode font), and can hold a very big number of item...internally or can be driven external (as you can see the Fileselector and TextViewer use own data structures, but displayed through the listbox).

  19. #19
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Each Listbox item with a different colour

    I think MartinLiss created a demo in the VB6 and Earlier codebank which showed how to do this.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

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