vba 限制 Access 中多选列表框中的选择数量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1406973/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Limit number of selections in a MultiSelect ListBox in Access?
提问by mandroid
Is there a way to limit the number of selections a user can choose on a ListBox with MultiSelect enabled in Access 2003? Right now I have a procedure that fires on the On Click event that checks the number of choices selected and if it is over my threshold it will display a warning label.
有没有办法限制用户可以在 Access 2003 中启用 MultiSelect 的 ListBox 上选择的选项数?现在我有一个程序在 On Click 事件上触发,该事件检查所选选项的数量,如果超过我的阈值,它将显示一个警告标签。
回答by Marcand
You can use the listbox BeforeUpdate event to see the listbox.ItemsSelected.Count value. If this is over your limit then you unselect (with listbox.Selectec(item)=False) the current one and cancel the event.
您可以使用列表框 BeforeUpdate 事件来查看 listbox.ItemsSelected.Count 值。如果这超出了您的限制,则您取消选择(使用 listbox.Selectec(item)=False)当前一个并取消该事件。
Here's an example :
Private Sub lstItems_BeforeUpdate(Cancel As Integer)
这是一个例子:
Private Sub lstItems_BeforeUpdate(Cancel As Integer)
' Warn the user that only x items can be selected.
' ------------------------------------------------
If lstItems.ItemsSelected.Count > MAX_SELECTED_ITEM_PERMITTED Then
MsgBox "You can only select " & MAX_SELECTED_ITEM_PERMITTED & " items in this list.", vbOKOnly + vbInformation, "Error"
' Unselect previously selected item.
' ----------------------------------
lstItems.Selected(lstItems.ListIndex) = False
Cancel = True
End If
End Sub
' Warn the user that only x items can be selected.
' ------------------------------------------------
If lstItems.ItemsSelected.Count > MAX_SELECTED_ITEM_PERMITTED Then
MsgBox "You can only select " & MAX_SELECTED_ITEM_PERMITTED & " items in this list.", vbOKOnly + vbInformation, "Error"
' Unselect previously selected item.
' ----------------------------------
lstItems.Selected(lstItems.ListIndex) = False
Cancel = True
End If
End Sub
回答by Fink
Give this a try. It basically deselects the last item selected if its over the predefined limit:
试试这个。如果超过预定义的限制,它基本上会取消选择最后一个选择的项目:
Private Sub ListBox1_Change()
Dim counter As Integer
Dim selectedCount As Integer
selectedCount = 0
For counter = 1 To ListBox1.ListCount Step 1
If ListBox1.Selected(counter - 1) = True Then 'selected method has 0 base
selectedCount = selectedCount + 1
End If
Next counter
If selectedCount >= 4 Then 'modify # here
ListBox1.Selected(ListBox1.ListIndex) = False 'listindex returns the active row you just selected
MsgBox "Limited to 4 Choices", vbInformation + vbOKOnly, "Retry:"
End If
End Sub
回答by David-W-Fenton
I would suggest that a much more manageable, user-friendly and understandable UI for this would be the paired listbox approach, with ADD> and button after the user has reached the limit. You don't have to do anything difficult, just check the ListCount of the right-hand listbox and disable the ADD> button when it reaches the limit.
我建议更易于管理、用户友好和易于理解的 UI 是配对列表框方法,在用户达到限制后使用 ADD> 和按钮。您不必做任何困难的事情,只需检查右侧列表框的 ListCount 并在达到限制时禁用 ADD> 按钮。
And you avoid many problems as it's quite clear to the user what they are doing in comparison to selecting multiple items at once in a single listbox. You could make the lefthand listbox multiselect and simply disable the ADD> button if the ItemsSelected count exceeds the limit, and notify the user appropriately.
而且您避免了许多问题,因为与在单个列表框中一次选择多个项目相比,用户很清楚他们在做什么。如果 ItemsSelected 计数超过限制,您可以使左侧列表框多选并简单地禁用 ADD> 按钮,并适当通知用户。
回答by Tony Toews
Using the listbox ItemsSelected collection? That's the only way that I know of to limit the number of selected items. OTOH there are some truly twisted individuals out there who might have figured out an alternative so I never say never.
使用列表框 ItemsSelected 集合?这是我所知道的限制所选项目数量的唯一方法。OTOH 有一些真正扭曲的人可能已经找到了替代方案,所以我永远不会说永远。
What's the problemw with this method?
这种方法有什么问题?