2

Estoy realizando una app en windows forms donde hago registros, pero validarme que algunos campos sean llenados estoy usando Error Provider, mas sin embargo cuando provoco el error me marcan todos excepto el DateTimePicker y un Textbox donde debo ingresar Int que error puede tener mi codigo?

 private void btnCargar_Click(object sender, EventArgs e)
    {
        //Metodo para limpiar los error provider(si anteriormente ya hubieran aparecido)
        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");
            }

            //En este campo es el text box que debe ir un int y no me aparece el error provider cuando no ingreso nada
            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 en el cual debo checar una fecha y si no deberia aparecer el error icon
            if (dtmpDateReceived.Checked == false)
            {
                ErrorIcono.SetError(dtmpDateReceived, "Ingrese una Fecha");
            }

        }

3 respuestas 3

1

En el código siguiente no hay error sintactico:

  //DateTimePicker en el cual debo checar una fecha y si no deberia aparecer el error icon
   if (dtmpDateReceived.Checked == false)
   {
     ErrorIcono.SetError(dtmpDateReceived, "Ingrese una Fecha");
   }

El error radica en que la propiedad dtmpDateReceived.Checked no es lo apropiado para detectar tu error, si el Error Provider no marca el error en el DateTimePicker es por que la condición dtmpDateReceived.Checked == false no se cumple, sencillamente por que dtmpDateReceived.Checked está establecida a true, dtmpDateReceived.Checked de manera predeterminada es true y solo cambia si la propiedad ShowCheckBox del DateTimePicker está establecida a true y es el usuario quien puede establecerla a false desmarcando el CheckBox. Si tu objetivo es validar la fecha colocada, debes emplear una condición adecuada al error que deseas evitar, por ejemplo:

if(dtmpDateReceived.Value.Date == DateTime.Now.Date)
{
  ErrorIcono.SetError(dtmpDateReceived, "La Fecha coincide con la fecha actual");
}

Te serviría para impedir que el usuario colocará la fecha actual, si el caso es que deseas cualquier fecha menos la del día actual.

1

podría probar con esto:

   //En este campo es el text box que debe ir un int y no me aparece el error provider cuando no ingreso nada
        if(txtTotalI.Text == "")
        {
            ErrorIcono.SetError(txtTotalI, "Ingrese un Numero");
        }
//DateTimePicker en el cual debo checar una fecha y si no deberia aparecer el error icon
        if (dtmpDateReceived.Checked = false)
        {
            ErrorIcono.SetError(dtmpDateReceived, "Ingrese una Fecha");
        }

No es necesario usar un string.empty, puedes preguntar si es vacio ""

1
  • No me funciono tampoco como me dijiste el 26 feb. 2020 a las 20:12
1

Para tu verificación puedes realizar lo siguiente, en ves de hacer un if() en general y después ir verificando uno a uno para mostrar el error, copia, remplaza los datos y pega el siguiente código las veces que sea necesario.

Strings:

If (string.IsNullOrEmpty(Campo.Text))
{
ErrorIcono.SetError(txtCustomer, "Ingrese....");
return;
}

Ints:

If (int.Parse(Campo.Text) >= 0)
{
ErrorIcono.SetError(txtCustomer, "Ingrese....");
return;
}

DateTimePickers:

If(Campo.Value == DateTime.UtcNow.Date)
{
ErrorIcono.SetError(txtCustomer, "Ingrese....");
return;
}

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.