limit selections in a listbox in vb.net

做~自己de王妃 提交于 2019-12-12 12:42:47

问题


I am creating a web control in vb.net that contains a list box. I know that I am able to choose a selection mode of Single or Multiple, but I am interested in limiting a Multiple selection to a predetermined size. For example preventing a user from selecting more than 3 items.

I have tried adding a Validator to the list box, which might be a way forward, but was wondering if there was a way of changing the selection mode to something a bit different than just the two specified ones.

Any ideas or suggestions would be greatly appreciated

Edit: Unfortunately due to project limitations I am unable to use Javascript on this problem. very annoying I know!


回答1:


You could try handling one of the SelectedIndexChange events. With multiple selection, each time this event fires you can check how many items have already been selected. If it is more than you want, have the handler deselect the one that was just selected. You could also put up a little red label under the listbox saying "Sorry, no more than [n] selections allowed."

EDIT: Just noticed you said WEB. Same theory applies, but it would have to be done using Javascript.




回答2:


On the SelectedIndexChanged event, write a short piece of code that will check the number of selected items and if it is greater than your desired amount, unselect the new selection (or unselect the oldest one, or however you wish this to operate).



来源:https://stackoverflow.com/questions/541883/limit-selections-in-a-listbox-in-vb-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!