No if/else
should not be possible, but you can use the &&
to do in a if
only:
private void maskedTextBoxEAN_Leave(object sender, CancelEventArgs e)
{
if (!string.IsNullOrWhiteSpace(maskedTextBoxEAN.Text) &&
!ValidaEAN13.CalculateChecksum(maskedTextBoxEAN.Text))
CaixaMensagemDeErro.Mensagem("O código EAN digitado não é valido.");
}
Depending on how the CalculateChecksum
method handles the argument, IsNullOrWhiteSpace
may not be necessary.
As a suggestion, you can use ErrorProvider
to inform the user of the error.
private void maskedTextBoxEAN_Leave(object sender, CancelEventArgs e)
{
if (!ValidaEAN13.CalculateChecksum(maskedTextBoxEAN.Text){
errorProvider1.SetError(maskedTextBoxEAN, "O código EAN digitado não é valido.");
//e.Cancel = true;
}
else
{
errorProvider1.SetError(maskedTextBoxEAN, String.Empty);
//e.Cancel = false;
}
}
You can also cancel the user action by setting true
to property Cancel
of the control.