This is the same as how most apps behave. I thought TreeView worked like that by default.
Is there a way to do this, or do I have to get all the children of a TreeNode that’s checked and check them myself?
This is winforms.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You need to do it yourself, which on the other hand is not very hard:
This takes care of both checking and unchecking all children (regardless of how many levels down there may be child nodes).
Update
Expanded code sample that will also check/uncheck parent node if all of its child nodes are checked or unchecked manually (not thoroughly tested, could probably be done more elegantly):
The if-block that is added in the
SetChildrenCheckedmethod will prevent aStackOverflowExceptionin the case where you check a node with child nodes, they get checked, and when the last one is checked the parent (the one you clicked on) gets cecked and triggers theAfterCheckevent again (which surprises me a bit; I would not expect the event to be raised when theCheckedproperty value does not change but rather just gets assigned the same value it already had, but then again the event is called AfterCheck, not AfterCheckedChanged).