Home » Controls » How to limit selections in a listbox control
How to limit selections in a listbox control
Posted on July 26, 2010
Sometimes you may require to limit the number of items which can be selected by the user in run time. Here is one of the way I can think of in VB6:
In this example, we are going to limit the number of selections in list box control to three. If the user tries to select more than three items then he will get a error message saying he can select maximum of 3 items only.
Instructions:
- Create a new Project
- Add a new Form to it and name it as Form1
- Add a list box control to it and name it as list1
Copy the following code to Form1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | Option Explicit Private Sub Form_Load() With List1 .AddItem "Test data 1" .AddItem "Test data 2" .AddItem "Test data 3" .AddItem "Test data 4" .AddItem "Test data 5" .AddItem "Test data 6" .AddItem "Test data 7" .AddItem "Test data 8" .AddItem "Test data 9" .AddItem "Test data 10" End With End Sub Private Sub List1_Click() If List1.SelCount > 3 Then Call MsgBox("You can select maximum of 3 items in your list box.", vbExclamation, App.Title) List1.Selected(List1.ListIndex) = False End If End Sub |
If you know any other method(s) of limiting the selections in a list box control or have any comments or questions, please post as a comment.
Thank you :)
Enjoy this article?
Filed under: Controls
Leave a comment