CheckBox concatenating string several times

0

Iwantedtoclickonthecheckboxandthenontheregister,savethealreadypredefinedvalueandservicename(description)inthedatabase.

However,thedescriptionisaproblemforme,becausewhentheusermarksandunchecksthecheckboxseveraltimes,itendsupconcatenatingmorethanexpected.

Andifitmarks,thenuncheck,thedescriptionstillcontainsthevalueoftheuncheckedoption.

IknowthishappensbecauseChecked==truewillhappenseveraltimes,Itriedtoconcatenateoutofmethod,butcouldnot.

Bank:  

    
asked by anonymous 28.02.2015 / 03:51

2 answers

2

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

Declare a List<string> to save each item in the description, then scroll through that 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 Register button method

//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();
    
28.02.2015 / 12:54
2

It is very simple to solve this and perhaps other problems that you did not realize happens because of the same error. But it does require architectural change.

The problem

The entire form, not just 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 it to generate the string to be used each time a click is given? The click should not generate such an action.

In fact, this is a common mistake. Programmers usually validate in the control itself, but in general validation should be done at the end of the registration process. Many times you can not even validate while 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 the specific control should be done outside of it, must be done when the process is being completed, when you are closing the form under normal conditions or some action determines the end of the registration process. This is valid for validations and preparation of data before writing to the database.

There is no reason to prepare data to write before the data is in a definitive state for recording.

Actually, something tells me that the problem is bigger than this. I still have doubts if I should mount a string to write to the database, it seems to me a wrong solution, but since 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 should take care of its real function, each one should have a single responsibility.

The solution

Then in the appropriate event you scan all the checkbox that need to be evaluated and mount the string you need. This will no longer be a problem.

You have a way of solving this in another way, but the most correct one is the one I gave you.

I believe the correct event to use is FormClosing but it's good to take a look at everyone and see if there are any more suitable. Eventually you can trigger this process via a "save" button as well. This is why the finalization process should probably be in a method that can be called from several places since there may be more than one way to terminate the registration process.

If you prefer to separate responsibilities, which is correct, then what you should do is upgrade the class that represents the given to being registered instead of worrying about the string that goes to the database (which should probably be written in a different way). In fact if this were done the checkbox event until could be used to update the object that holds the data. It would not be the right one yet but it would work.

    
28.02.2015 / 12:50