|
-
Aug 10th, 2006, 02:09 PM
#1
Thread Starter
New Member
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?
-
Aug 10th, 2006, 02:35 PM
#2
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:
Option Explicit
Private Sub ListView1_ItemClick(ByVal Item As MSComctlLib.ListItem)
'don't let the user select 2nd item
If Item.Index = 2 Then Item.Selected = False
End Sub
Private Sub Form_Load()
' Just for testing ==>
Dim i As Long
Dim tmpLstitem As ListItem
Dim lForeColor As Long
With ListView1
.View = lvwReport
.MultiSelect = True
.ColumnHeaders.Add , , "Col_1"
.ColumnHeaders.Add , , "Col_2"
.ColumnHeaders.Add , , "Col_3"
'
For i = 1 To 10
Set tmpLstitem = .ListItems.Add(, , "Item_" & i)
tmpLstitem.ListSubItems.Add , , "Subitem_1"
tmpLstitem.ListSubItems.Add , , "Subitem_2"
'
If i = 2 Then
lForeColor = vbGrayText 'Grayout the 2nd item
Else
lForeColor = vbAutomatic
End If
tmpLstitem.ForeColor = lForeColor
tmpLstitem.ListSubItems(1).ForeColor = lForeColor
tmpLstitem.ListSubItems(2).ForeColor = lForeColor
Next
End With
End Sub
Last edited by iPrank; Aug 10th, 2006 at 02:54 PM.
Reason: forgot the Color part
-
Aug 10th, 2006, 02:47 PM
#3
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:
Option Explicit
Dim i As Integer
Dim selectedItems As Integer
Private Sub Form_Load()
List1_Click
End Sub
Private Sub List1_Click()
For i = 0 To List1.ListCount - 1
If List1.Selected(i) = False Then
List1.ForeColor = vbButtonFace
Else
selectedItems = selectedItems + 1
List1.ForeColor = vbBlack
End If
If selectedItems = 0 Then
List1.ForeColor = vbBlack
End If
Next i
End Sub
Private Sub List1_LostFocus()
If selectedItems = 0 Then
List1.ForeColor = vbBlack
End If
End Sub
-
Aug 10th, 2006, 03:03 PM
#4
Thread Starter
New Member
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?
-
Aug 10th, 2006, 03:07 PM
#5
Thread Starter
New Member
Re: List box question
Thanks! I'll give that a shot. My program is flexible enough that I work with listview instead.
-
Aug 10th, 2006, 03:12 PM
#6
Re: List box question
Of course. It's a much better control than ListBox.
-
Aug 10th, 2006, 03:20 PM
#7
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|