Поставщик ошибок для DateTimePicker и текстового поля - PullRequest
0 голосов
/ 27 февраля 2020

Я запускаю приложение в Windows формах, где я делаю записи, но проверяю, что некоторые поля заполнены. Однако я использую Error Provider, однако, когда я вызываю ошибку, они отмечают меня все, кроме DateTimePicker и Textbox где я должен ввести 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");
        }

    }

Ответы [ 2 ]

0 голосов
/ 27 февраля 2020

Используйте оператор else, если это будет go, чтобы шаг за шагом проверять

или l oop по всем текстовым полям и проверять ВСЕ

Foreach(TextBox txt in this.Controls.OfType<TextBox>())
{
 if (string.IsNullOrWhitespace(txt.Text))
{
 errorprovider1.SetError(txt, "Empty Field");
}
}
0 голосов
/ 27 февраля 2020

Чтобы определить, имеет ли текст, введенный в textBox, тип int, вы можете использовать метод Int32.TryParse.

int num;
bool success = int.TryParse(textBox1.Text, out num);
if (!success)
{
    errorProvider1.BlinkStyle = ErrorBlinkStyle.BlinkIfDifferentError;
    errorProvider1.SetIconPadding(this.textBox1, 5);
    errorProvider1.SetError(this.textBox1, "Must be an int");
}
else
{
    errorProvider1.SetError(this.textBox1, "");
}

Кроме того, я не знаю, почему нужно судить о DataTimePicker. Он выбирает дату по умолчанию.

...