code_junkie

Limiting a group of checkboxes to a certain amount of checks

Nov 11th, 2011
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. private Queue<CheckBox> favAttributesLimiter - new Queue<CheckBox>();
  2. private const int MaxFavoredAttributes = 5;
  3.  
  4. private void favoredAttributes_CheckedChanged(object sender, EventArgs e)
  5. {
  6. CheckBox cb = (CheckBox)sender;
  7.  
  8. if (cb.Checked)
  9. {
  10. if (favAttributesLimiter.Count == MaxFavoredAttributes)
  11. {
  12. CheckBox oldest = favAttributesLimiter.Dequeue();
  13.  
  14. oldest.CheckedChanged -= favoredAttributes_CheckedChanged;
  15. oldest.Checked = false;
  16. oldest.CheckedChanged += new EventHandler(favoredAttributes_CheckedChanged);
  17. }
  18.  
  19. favAttributesLimiter.Enqueue(cb);
  20.  
  21. }
  22. else // cb.Checked == false
  23. {
  24. if (favAttributesLimiter.Contains(cb))
  25. {
  26. var list = favAttributesLimiter.ToList();
  27. list.Remove(cb);
  28. favAttributesLimiter=new Queue<CheckBox>(list);
  29. }
  30. }
  31. }
Add Comment
Please, Sign In to add comment