Results 1 to 4 of 4

Thread: Combo Box question

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2002
    Posts
    70

    Combo Box question

    If I have a combo box with 10 items in it's list, is it possible to make the 5th item in the list bold or maybe a different color than the rest of the items in the list?

    Thanks

  2. #2
    Hyperactive Member Knowledge_is_Et's Avatar
    Join Date
    Dec 2001
    Location
    An Oak.
    Posts
    305
    I don't believe so. I am trying to find (or make) a control for listboxes and comboboxes that will do that, but no luck yet.
    Now returning to the world of VB. Please make sure your seatbelts are securely fastened and all trays are in their upright and locked position.

  3. #3
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217
    you could perhaps make it stand out, like

    -------Header--------
    Remember, if someone's post was not helpful, you can always rate their post negatively .

  4. #4
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    Well

    Form the all-time Guru Aaron Young :

    VB Code:
    1. ' In a module
    2.  
    3. Option Explicit
    4.  
    5. Public Type RECT
    6.         Left As Long
    7.         Top As Long
    8.         Right As Long
    9.         Bottom As Long
    10. End Type
    11.  
    12. Public Type DRAWITEMSTRUCT
    13.         CtlType As Long
    14.         CtlID As Long
    15.         itemID As Long
    16.         itemAction As Long
    17.         itemState As Long
    18.         hwndItem As Long
    19.         hdc As Long
    20.         rcItem As RECT
    21.         itemData As Long
    22. End Type
    23.  
    24. Public Type CWPSTRUCT
    25.         lParam As Long
    26.         wParam As Long
    27.         message As Long
    28.         hwnd As Long
    29. End Type
    30.  
    31. Public Type CREATESTRUCT
    32.         lpCreateParams As Long
    33.         hInstance As Long
    34.         hMenu As Long
    35.         hWndParent As Long
    36.         cy As Long
    37.         cx As Long
    38.         y As Long
    39.         x As Long
    40.         style As Long
    41.         'These next 2 are Normaly String, but need to be a fixed length
    42.         'so we know how long they are when using CopyMemory,
    43.         'We're only interested in the Style Property anyway.
    44.         lpszName As Long
    45.         lpszClass As Long
    46.         ExStyle As Long
    47. End Type
    48.  
    49. Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
    50. Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    51. 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
    52. 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
    53. Public Declare Function SetBkColor Lib "gdi32" (ByVal hdc As Long, ByVal crColor As Long) As Long
    54. Public Declare Function SetTextColor Lib "gdi32" (ByVal hdc As Long, ByVal crColor As Long) As Long
    55. 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
    56. Public Declare Function GetSysColor Lib "user32" (ByVal nIndex As Long) As Long
    57. Public Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
    58. Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
    59. Public Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long
    60. Public Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
    61. Public Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal ncode As Long, ByVal wParam As Long, lParam As Any) As Long
    62.  
    63. Public Const WH_CALLWNDPROC = 4
    64.  
    65. Public Const CBS_OWNERDRAWVARIABLE = &H20&
    66. Public Const CB_GETLBTEXT = &H148
    67. Public Const CB_SETITEMHEIGHT = &H153
    68.  
    69. Public Const COLOR_HIGHLIGHT = 13
    70. Public Const COLOR_HIGHLIGHTTEXT = 14
    71. Public Const COLOR_WINDOW = 5
    72. Public Const COLOR_WINDOWTEXT = 8
    73.  
    74. Public Const GWL_WNDPROC = (-4)
    75. Public Const GWL_STYLE = (-16)
    76.  
    77. Public Const ODS_SELECTED = &H1
    78.  
    79. Public Const ODT_COMBOBOX = 3
    80.  
    81. Public Const WM_CREATE = &H1
    82. Public Const WM_DRAWITEM = &H2B
    83.  
    84. Public lPrevWndProc As Long
    85. Public lHook As Long
    86. Public lSubCombo As Long
    87.  
    88. Public Function SubClassedForm(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    89.     Dim tItem As DRAWITEMSTRUCT
    90.     Dim sItem As String
    91.    
    92.     If Msg = WM_DRAWITEM Then
    93.         'This function only passes the Address of the DrawItem Structure, so we need to
    94.         'use the CopyMemory API to Get a Copy into the Variable we setup:
    95.         Call CopyMemory(tItem, ByVal lParam, Len(tItem))
    96.         'If it's our Combobox..
    97.         If tItem.CtlType = ODT_COMBOBOX Then
    98.             'Get the Item Text
    99.             sItem = Space(255)
    100.             Call SendMessage(tItem.hwndItem, CB_GETLBTEXT, tItem.itemID, ByVal sItem)
    101.             sItem = Left(sItem, InStr(sItem, Chr(0)) - 1)
    102.             'Select the Highlight Colors if this Item is currently selected
    103.             If (tItem.itemState And ODS_SELECTED) Then
    104.                 Call SetBkColor(tItem.hdc, GetSysColor(COLOR_HIGHLIGHT))
    105.                 Call SetTextColor(tItem.hdc, GetSysColor(COLOR_HIGHLIGHTTEXT))
    106.             Else
    107.                 'Otherwise, use the default Colors, unless the Item Data contains a new Color
    108.                 Call SetBkColor(tItem.hdc, GetSysColor(COLOR_WINDOW))
    109.                 Call SetTextColor(tItem.hdc, IIf(tItem.itemData, tItem.itemData, GetSysColor(COLOR_WINDOWTEXT)))
    110.             End If
    111.             'Display the Item
    112.             TextOut tItem.hdc, tItem.rcItem.Left, tItem.rcItem.Top, ByVal sItem, Len(sItem)
    113.             'Don't Return a Value as we've dealt with this Message ourselves
    114.             SubClassedForm = 0
    115.             Exit Function
    116.         End If
    117.     End If
    118.     'Not our Combobox, so just process the Message as Normal
    119.     SubClassedForm = CallWindowProc(lPrevWndProc, hwnd, Msg, wParam, lParam)
    120. End Function
    121.  
    122. Sub Main()
    123.     'The Combobox is a little more tricky to manipulate than a Listbox
    124.     'So we need to do a little extra work to make it an "Owner Drawn" Control.
    125.     lHook = SetWindowsHookEx(WH_CALLWNDPROC, AddressOf HookApp, App.hInstance, App.ThreadID)
    126.     Form1.Show
    127.     'Once the Control. etc are Drawn, we can release the Hook
    128.     Call UnhookWindowsHookEx(lHook)
    129. End Sub
    130.  
    131. Private Function HookApp(ByVal lHookID As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    132.  
    133.     'This Function will get called when Initializing the Form
    134.     'We want to Interupt it when it tries to create our Combobox..
    135.     Dim tCWP As CWPSTRUCT
    136.     Dim sClass As String
    137.  
    138.     Call CopyMemory(tCWP, ByVal lParam, Len(tCWP))
    139.  
    140.     If tCWP.message = WM_CREATE Then
    141.         'Get the Control Classname
    142.         sClass = Space(128)
    143.         Call GetClassName(tCWP.hwnd, ByVal sClass, 128)
    144.         sClass = Left(sClass, InStr(sClass, Chr(0)) - 1)
    145.         'If it's our Combobox, Sub-class it to Modify the Create Message..
    146.         If sClass = "ComboLBox" Then
    147.             lSubCombo = SetWindowLong(tCWP.hwnd, GWL_WNDPROC, AddressOf SubComboCreate)
    148.         End If
    149.     End If
    150.     'Continue the Hook Processing
    151.     HookApp = CallNextHookEx(lHook, lHookID, wParam, ByVal lParam)
    152.  
    153. End Function
    154.  
    155. Private Function SubComboCreate(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    156.     'This Function will be called when the Combobox is about to be created
    157.     Dim tCreate As CREATESTRUCT
    158.    
    159.     If Msg = WM_CREATE Then
    160.         'Grab the Data that's going to be used to Create the Combobox
    161.         Call CopyMemory(tCreate, ByVal lParam, Len(tCreate))
    162.         'Alter it, to make the Combobox an "Owner Drawn" Control
    163.         tCreate.style = tCreate.style Or CBS_OWNERDRAWVARIABLE
    164.         'Copy the modified data back
    165.         Call CopyMemory(ByVal lParam, tCreate, Len(tCreate))
    166.         'Alter the Style to OwnerDrawn
    167.         Call SetWindowLong(hwnd, GWL_STYLE, tCreate.style)
    168.         'Release this Subclassing Function
    169.         Call SetWindowLong(hwnd, GWL_WNDPROC, lSubCombo)
    170.     End If
    171.     'Let Windows Process the Modified Data
    172.     SubComboCreate = CallWindowProc(lSubCombo, hwnd, Msg, wParam, lParam)
    173.    
    174. End Function

    Never used it, just found it...
    Last edited by James Stanich; Jun 21st, 2002 at 03:42 PM.
    Remaining quiet down here !!!

    BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....

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