【问题标题】:Error Provider for a DateTimePicker and a TextboxDateTimePicker 和文本框的错误提供程序
【发布时间】: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


【解决方案1】:

使用else if语句会一步一步去检查

或者遍历所有文本框并检查所有

Foreach(TextBox txt in this.Controls.OfType<TextBox>())
{
 if (string.IsNullOrWhitespace(txt.Text))
{
 errorprovider1.SetError(txt, "Empty Field");
}
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-08
    • 1970-01-01
    • 2017-04-26
    • 1970-01-01
    • 2018-03-05
    • 2020-08-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多