Results 1 to 7 of 7

Thread: List box question

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2006
    Posts
    3

    List box question

    I am working on a program that includes many things in a list box. However, I only want certain items to be selectable based on the some prereqs I will set. The items that are not selectable, I still want to show up in the list, just grayed out like when you disable a control. Any ideas on how to do this?

  2. #2
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,729

    Re: List box question

    Hi ! Welcome to VBForums.

    Sorry, I don't know if it is possible with ListBox.
    But here is an alternative using ListView. If it is possible to change your design, you may give it a try.
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub ListView1_ItemClick(ByVal Item As MSComctlLib.ListItem)
    4.   'don't let the user select 2nd item
    5.   If Item.Index = 2 Then Item.Selected = False
    6. End Sub
    7.  
    8. Private Sub Form_Load()
    9.   ' Just for testing ==>
    10.   Dim i As Long
    11.   Dim tmpLstitem As ListItem
    12.   Dim lForeColor As Long
    13.  
    14.   With ListView1
    15.     .View = lvwReport
    16.     .MultiSelect = True
    17.     .ColumnHeaders.Add , , "Col_1"
    18.     .ColumnHeaders.Add , , "Col_2"
    19.     .ColumnHeaders.Add , , "Col_3"
    20.     '
    21.     For i = 1 To 10
    22.       Set tmpLstitem = .ListItems.Add(, , "Item_" & i)
    23.       tmpLstitem.ListSubItems.Add , , "Subitem_1"
    24.       tmpLstitem.ListSubItems.Add , , "Subitem_2"
    25.       '
    26.       If i = 2 Then
    27.         lForeColor = vbGrayText 'Grayout the 2nd item
    28.       Else
    29.         lForeColor = vbAutomatic
    30.       End If
    31.      
    32.       tmpLstitem.ForeColor = lForeColor
    33.       tmpLstitem.ListSubItems(1).ForeColor = lForeColor
    34.       tmpLstitem.ListSubItems(2).ForeColor = lForeColor
    35.     Next
    36.   End With
    37. End Sub
    Last edited by iPrank; Aug 10th, 2006 at 02:54 PM. Reason: forgot the Color part
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


  3. #3
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: List box question

    Welcome to the forums

    I think this does something like you've asked for. However, you can't use multiple font colors in the ListBox control:

    VB Code:
    1. Option Explicit
    2.     Dim i As Integer
    3.     Dim selectedItems As Integer
    4.  
    5. Private Sub Form_Load()
    6.     List1_Click
    7. End Sub
    8.  
    9. Private Sub List1_Click()
    10. For i = 0 To List1.ListCount - 1
    11.     If List1.Selected(i) = False Then
    12.         List1.ForeColor = vbButtonFace
    13.     Else
    14.         selectedItems = selectedItems + 1
    15.         List1.ForeColor = vbBlack
    16.     End If
    17.         If selectedItems = 0 Then
    18.             List1.ForeColor = vbBlack
    19.         End If
    20. Next i
    21. End Sub
    22.  
    23. Private Sub List1_LostFocus()
    24.     If selectedItems = 0 Then
    25.         List1.ForeColor = vbBlack
    26.     End If
    27. End Sub

  4. #4

    Thread Starter
    New Member
    Join Date
    Aug 2006
    Posts
    3

    Re: List box question

    Thanks Gavio, but the user will still be able to select those items that have the vbButtonFace color won't they?

  5. #5

    Thread Starter
    New Member
    Join Date
    Aug 2006
    Posts
    3

    Re: List box question

    Thanks! I'll give that a shot. My program is flexible enough that I work with listview instead.

  6. #6

  7. #7
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,729

    Re: List box question

    BTW, I've NEVER used ListBox in any of my projects.
    That's why I don't know much about ListBox.
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


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