问题
I have an multiselect listbox that i need to disable users from selecting certain items in the listbox. I've tried looking at the listbox enable but that seems to be for the whole listbox only. Any ideas? Here's my listbox, i currently is looping through the item hoping to disable the item:
Dim lItem As Long
For lItem = 0 To Sheet1.ListBox1.ListCount - 1
If Sheet1.ListBox1.Selected(lItem) Then
Sheet1.ListBox1.Enabled(lItem) = False ' don't work?
End If
Next
回答1:
For it = 0 To Sheet1.ListBox1.ListIndex
If Sheet1.ListBox1.Selected(it) Then
Sheet1.ListBox1.Selected(it) = False
Sheet1.ListBox1.RemoveItem (it)
End If
Next
Line1 : goes through all the indexes of the listbox
Line2 : find the selected item
Line3 : deselects
Line4 : removes the item selected
回答2:
Option 1: This takes the user's selection and effectively remove it, while retaining the original ListIndex for all items.
Listbox1.List(MyListbox1.ListIndex) = " "
Then you can use code to check for a value of " "
Option 2: Move Listbox1.List(Listbox1.ListIndex)
from one column (visible) to another that is not (width = 0). This would also retain the original listindex order and the value if you need to reinstate it (make it a valid option).
Option 3: To get the illusion of grayed out rows, as long as there are no scroll bars, you could place borderless labels with a gray background over the listbox. The .top value would need to be calculated using the listbox's .top plus X * ListIndex to place it directly over the non-available option. Then make the label's caption = to the listbox selection. (I never said it was easy, just that it was possible)
来源:https://stackoverflow.com/questions/50003702/disable-selecting-certain-items-in-listbox