joseph
joseph

Reputation: 37

TextBox and ComboBox validation not working as intended

I want to validate multiple TextBoxes and Comboboxes with a MessageBox. I have done that, but the problem now is that once I fill Jtextbox3and ComboBox1 it ignores the other TextBoxes andComboBoxes and goes to the next form.

Here is my code:

private void bunifuFlatButton1_Click(object sender, EventArgs e)      
{
    userform3 form3 = new userform3();
    var jtextboxes = new[] { jTextBox3, jTextBox4, jTextBox5, jTextBox6, jTextBox7, jTextBox8, jTextBox9, jTextBox10, jTextBox11, jTextBox12, jTextBox13, jTextBox14, jTextBox15, };
    var comboboxes = new[] { comboBox1, comboBox2, comboBox3, comboBox4, comboBox5, comboBox6 };
    foreach (var jbox in jtextboxes)
    {
        foreach (var combo in comboboxes)
        {
            if (string.IsNullOrEmpty(jbox.TextValue) || combo.SelectedItem == null)
            {
                MetroFramework.MetroMessageBox.Show(this, "", "Please Enter All the Fields as Required", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            else
            {
                form3.Show();
                this.Hide();
                form3.Hide();
                form3.ShowDialog();
                this.Show();
            }

            break;
        }

        break;
    }
}

This is the form. I have marked the TextBox and ComboBox that I have a problem with.

Upvotes: 0

Views: 143

Answers (2)

Joshua Robinson
Joshua Robinson

Reputation: 3539

You're only checking the first TextBox and the first ComboBox. You have a break statement at the end of both foreach loops, so each loop will only execute one time... in other words you're only ever validating the first item in jtextboxes and comboboxes.

Furthermore, the inner loop is written in such a way that the code hiding / showing the forms will execute as soon as you encounter a TextBox with a value and a ComboBox with a value. A different way to write this might be:

private void bunifuFlatButton1_Click(object sender, EventArgs e)      
{
    userform3 form3 = new userform3();
    var jtextboxes = new[] { jTextBox3, jTextBox4, jTextBox5, jTextBox6, jTextBox7, jTextBox8, jTextBox9, jTextBox10, jTextBox11, jTextBox12, jTextBox13, jTextBox14, jTextBox15, };
    var comboboxes = new[] { comboBox1, comboBox2, comboBox3, comboBox4, comboBox5, comboBox6 };
    if (jtextboxes.Any(tb => string.IsNullOrEmpty(tb.TextValue)) || comboboxes.Any(cb => cb.SelectedItem == null))
    {
        MetroFramework.MetroMessageBox.Show(this, "", "Please Enter All the Fields as Required", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
    }
    else
    {
        form3.Show();
        this.Hide();
        form3.Hide();
        form3.ShowDialog();
        this.Show();
    }
}

This will show the message box if any of the TextBoxes are empty, or if any of the ComboBoxes don't have a selected value.

Upvotes: 1

sr28
sr28

Reputation: 5106

Looks like you have your 'break' in the wrong place for both the foreach loops. Break means that it will break out of the loop. So in your code, you're breaking out of the inner foreach loop after Combobox1 and then breaking out of the outer foreach after your first textbox. Remove those and it should work fine

Upvotes: 0

Related Questions