【发布时间】:2020-02-26 21:09:25
【问题描述】:
我在创建记录的 Windows 表单中运行应用程序,但验证是否填写了某些字段。我正在使用错误提供程序,但是,当我导致错误时,它们将我标记为除 DateTimePicker 和文本框之外的所有内容必须输入 Int 我的代码会出现什么错误?
private void btnCargar_Click(object sender, EventArgs e)
{
// Method to clear the error provider (if they had already appeared before)
LimpiarErrorICono();
if (txtSalesOrderI.Text == string.Empty || txtModelo.Text == string.Empty || txtCustomer.Text == string.Empty || txtTotalI.Text == string.Empty || cmbPriorityI.Text == string.Empty
|| cmbPriorityStatus.Text == string.Empty || dtmpDateReceived.Checked)
{
this.MensajeError("Faltan Ingresar Datos");
if (txtSalesOrderI.Text == string.Empty)
{
ErrorIcono.SetError(txtSalesOrderI, "Ingrese un SalesOrder");
}
if (txtCustomer.Text == string.Empty)
{
ErrorIcono.SetError(txtCustomer, "Ingrese un Customer");
}
if(txtModelo.Text == string.Empty)
{
ErrorIcono.SetError(txtModelo, "Ingrese un Modelo");
}
// In this field it is the text box that an int must go and I don't see
the error provider when I don't enter anything
if(txtTotalI.Text == string.Empty)
{
ErrorIcono.SetError(txtTotalI, "Ingrese un Numero");
}
if(cmbPriorityI.Text == string.Empty)
{
ErrorIcono.SetError(cmbPriorityI, "Ingrese una Prioridad");
}
if (cmbPriorityStatus.Text == string.Empty)
{
ErrorIcono.SetError(cmbPriorityStatus, "Ingrese una Estatus");
}
// DateTimePicker in which I must check a date and if the error should not appear icon
if (dtmpDateReceived.Checked == false)
{
ErrorIcono.SetError(dtmpDateReceived, "Ingrese una Fecha");
}
}
【问题讨论】:
-
1) 第一次 looong
if检查是不必要的,因为您正在检查同样的事情。 2)要清除ErrorProvider,只需在第一行执行MensajeError.Clear();。 3)检查TextBox-es,例如:if (txtTotalI.Text.Trim() == string.Empty) { .. }或if (txtTotalI.TextLength == 0) { }4)至于DateTimePicker,您的代码不会检查它,因为在第一个长ifs检查你有|| dtmpDateReceived.Checked。所以摆脱冗长的ifs检查。
标签: c# visual-studio textbox int datetimepicker