C
A suggestion to resolve this is as follows:Use the event Leave which occurs when the textbox is abandoned or loses focus.Create a method ContadorDeBultos to check the textbox txtbCantTubo.., count and assign the result to txtbBultosTotal.Invoking the method ContadorDeBultos at the event LeaveFor example: //Este es el método contador de bultos
private void ContadorDeBultos()
{
int cantidad = 0;
cantidad += (txtbCantTubo1.Text.Length > 0) ? 1 : -1;
// Ten en cuenta que la instrucción anterior es equivalente a
// if (txtbCantTubo1.Text.Length > 0)
// {
// cantidad++;
// }
// else
// {
// cantidad--;
// }
cantidad += (txtbCantTubo2.Text.Length > 0) ? 1 : -1;
cantidad += (txtbCantTubo3.Text.Length > 0) ? 1 : -1;
cantidad += (txtbCantTubo4.Text.Length > 0) ? 1 : -1;
cantidad += (txtbCantTubo5.Text.Length > 0) ? 1 : -1;
cantidad += (txtbCantTubo6.Text.Length > 0) ? 1 : -1;
//esta comprobación es por si se pasean por todos los textbox sin escribir nada
if(cantidad < 0)
{
cantidad = 0;
}
txtbBultosTotal.Text = cantidad.ToString();
}
Already with the method that makes the account, what is missing is to invoke it in the events private void txtbCantTubo1_Leave(object sender, EventArgs e)
{
ContadorDeBultos();
}
I'm telling you, in this case, you can assign the same event handler in all of the txtbCantTubo....As an additional control measure, you can invoke the method ContadorDeBultos() anywhere else you consider necessary. For example, in a general validation method prior to saving the information.It should be noted that my proposal is a possible solution, but it is not the only one.Update (subscriptions to events)I suspect that the mistakes you mention in your comment are related to the subscription to events. That operation you describe makes me think that the subscription is missing in the first 3.In addition to creating the method txtbCantTubo1_Leave, it is also necessary to establish a kind of mapping between the method and the corresponding event.To do that, select the txtbCantTubo... (one at a time) and check in the property window, in the event section, that the Leave event is associated with the corresponding method, as shown by the following image: You can create a single method txtbCantTubo_Leave and associate it in the 6 txtbCantTubo. But the association must be in the 6.