Hi
I have a ListBox control ListBox1 and my goal is that the user should only be able to 3 items in ListBox1. I don't want the user to be able to select a 4th item.
My question is how do I stop the user from selecting the 4th item in the ListBox control?
This is what I've tried so far:
I'm using the change event of the ListBox control to do the following:
1) I have made a loop that checks how many items are selected. The amount of items selected is stored in the variable intAmtSelected.
2) If 3 or more items are selected the ListBox control is disabled.
3) If less than 3 items are selected the ListBox control is again enabled.
4) If more than 3 items are selected it displays an error message
My problem is that the ListBox.Enabled function does not work, the user can still select more than 3 items in ListBox1. Anyone know of alternative ways to solve this?
I have a ListBox control ListBox1 and my goal is that the user should only be able to 3 items in ListBox1. I don't want the user to be able to select a 4th item.
My question is how do I stop the user from selecting the 4th item in the ListBox control?
This is what I've tried so far:
I'm using the change event of the ListBox control to do the following:
1) I have made a loop that checks how many items are selected. The amount of items selected is stored in the variable intAmtSelected.
2) If 3 or more items are selected the ListBox control is disabled.
3) If less than 3 items are selected the ListBox control is again enabled.
4) If more than 3 items are selected it displays an error message
My problem is that the ListBox.Enabled function does not work, the user can still select more than 3 items in ListBox1. Anyone know of alternative ways to solve this?
Code:
Private Sub ListBox1_Change()
Dim intAmtSelected As Integer
Dim intA As Long
'Checks if there is more than 3 items selected
For intA = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(intA) = True Then
intAmtSelected = intAmtSelected + 1
End If
Next intA
'If more than 3 items are selected disables the ListBox control and prompts an error message
If intAmtSelected > 3 Then
MsgBox "You are not allowed to select more than 3 items in total."
End If
'If more than 3 items are selected disable the listbox
If intAmtSelected >= 3 Then
ListBox1.Enabled = False
End If
'if less than 3 items are selected enable the lisbox
If intAmtSelected < 3 Then
ListBox1.Enabled = True
End If
End Sub