Checkbox concatenating string several times

Asked

Viewed 457 times

-1

Aplicaçao

I wanted to click on checkbox and then register, save the default value and service name in the database (description).

However, the description is a problem for me, because when the user marks and unchecks several times the checkbox, he ends up concatenating more than expected.

And if it checks, then uncheck, the description still contains the value of the unchecked option.

I know this happens because Checked == true will happen several times,I tried to carry out the concatenation outside the method, but I could not.

SS

Bench : SS

  • 1

    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.

2 answers

2

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.

2


Assuming descricao_servico is only required when the register button is clicked.

Declare a List<string> to save each description item, then scroll through this list to compose the description:

List<string> itensdescrição = new List<string>();

In each of the Checkedchanged methods:

if(cb_serv_pe.Checked)
{
    itensDescricao.Add("unha pé");
    this.servico = this.servico + 1;
}
else
{
    itensDescricao.Remove("unha pé");
    this.servico = this.servico - 1;
}

In the button method Register

//Limpa a descrição
descricao_servico = "";

//Percorre a lista e compõe a descrição
foreach (var item in itensDescrição)
{
    descricao_servico = descricao_servico + item + ",";
}

//Remove a última virgula
descricao_servico = descricao_servico.Remove(descricao_servico.Length - 1);

//Limpa a lista caso seja necessário utilizá-la outra vez
itensDescricao.Clear();
  • The string mount part is just one string.Join(",", itensDescrição), n?

  • @Francis, yes, you can use this instead of foreach used in response.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.