It’s very simple to solve this and maybe other problems that you didn’t realize happen because of the same mistake. But it requires an architectural change.
The problem
Every form, not only this specific control, has several events. Each has a purpose. Before using each one you should ask yourself if that event is really what you want to use as a trigger for some action.
Why do you need him to manage the string to be used each time a click is given? The click should not generate an action like this.
Actually, that’s a common mistake. Programmers usually validate in their own control, but in general the validation should be done at the end of the registration process. Often you can’t even validate right while you’re in the middle of the register. How many times I was angry because the form did not let me leave a control that was not valid but for me to leave valid I needed to go to another control.
Control events should be used for actions that can only be made within it. Anything that can be done outside of the specific control, should be done outside of it, should be done when the process is concluding, when closing the form under normal conditions or some action determines the end of the registration process. This is true for validations and data preparation before writing to the database.
There is no reason to prepare data to record before the data is in a definite state for recording.
Actually something tells me that the problem is bigger than this. I still have doubts whether I should mount a string to record in the database, seems to me a wrong solution, but as it is not the focus of the question I will let you think about it. Especially it seems to me something wrong to do during the form. Probably the form should update a class with the data and not worry about data persistence. Each one should take care of his real function, each one should have only one responsibility.
The solution
So in the proper event you sweep up all the checkbox which need to be evaluated and string you need. So you won’t have this problem anymore.
There’s a way to solve this another way but the right way is the one I gave you.
I believe the correct event to be used is the FormClosing
but it’s good to take a look at everyone and see if there are any more suitable ones. Eventually you can fire this process through a "save" button as well. This is probably why the completion process should be in a method that can be called multiple places since there may be more than one way to end the registration process.
If you prefer to separate the responsibilities, which is the correct one, then what you should do is update the class that represents the data to be registered instead of worrying about the string which goes into the database (which should probably be saved otherwise). Actually if this was done the event of the checkbox until could be used to update the object holding the data. It would not yet be the most correct but would work.
When putting code, prefer to post as text and not as image. Helps people take advantage of your code in some cases to better help you. It also helps other people find what they need in their question when using search engines.
– Maniero