I would strongly advise you to review your UI design instead. This control is really designed to represent arbitrary selection from 0 to N items, where N is the total number of items. When you try to break this design, the component will strongly resist. Worse, if you succeed, it will be way too confusing to the users who usually expect default and reasonable behavior.
I think at least one more reasonable design is nearly obvious. Instead of having a list box, have a set of combo boxes. Even better, it would be ideal to use an instance of the class
System.Web.UI.WebControls.TreeView
with combo-boxes on nodes. All you would need is to set the property
ShowCheckBoxes
and use the property
CheckedNodes
:
Please see:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.treeview%28v=vs.110%29.aspx[
^],
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.treeview.showcheckboxes%28v=vs.110%29.aspx[
^],
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.treeview.checkednodes%28v=vs.110%29.aspx[
^].
Now, you want to limit the number of checks. You will need to handle the event
TreeNodeCheckChanged
. Create some other control and display the checking status, for example "0 of 3 values checked", "1 of 3 values checked"… "3 of 3 values checked". It will provide the user with some feedback, it will become apparent that limited number of checks would be allowed. Always allow unchecking, but block checking more nodes above your limit.
—SA