c# - What algorithm is used in this code? -
this code checks , unchecks child nodes of treeview control. algorithm used in code?
private int _callcountup; private int _callcountdn; private void tvwpermissions_aftercheck(object sender, system.windows.forms.treevieweventargs e) { bool anychecked = false; if (_callcountdn == 0 && e.node.parent != null) { anychecked = false; foreach (treenode childnode in e.node.parent.nodes) { if (childnode.checked) { anychecked = true; break; } } _callcountup += 1; if (anychecked) e.node.parent.checked = true; _callcountup -= 1; } if (_callcountup == 0) { foreach (treenode childnode in e.node.nodes) { _callcountdn += 1; childnode.checked = e.node.checked; _callcountdn -= 1; } } }
not sure has name. quite standard, _callcountup/dn fields avoid trouble when changing checked property of node causes aftercheck event handler run again. stackoverflow typical outcome when event handler recurses without bound.
the generic pattern resembles this:
private bool modifyingnodes; private void treeview_aftercheck(object sender, treevieweventargs e) { if (modifyingnodes) return; modifyingnodes = true; try { // etc.. } { modifyingnodes = false; } }
the block ensures handled exception (such through threadexceptiondialog) doesn't permanently leave state variable set true. it's optional of course.
Comments
Post a Comment