I have a form that comprises 16 jtextboxes
and 6 comboBoxes
, and I want to validate all of them at once, or at least all jtextboxes
at once and all comboBoxes
at once. I have done that, but the problem is I use MessageBox
as the validation to all of them and the MessageBox
keeps on showing up after clicking on "Ok" or "Cancel".
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, jTextBox16
};
var comboboxes = new[] {
comboBox1, comboBox2, comboBox3, comboBox4, comboBox5, comboBox6
};
foreach(var jbox in jtextboxes) {
foreach(var combo in comboboxes) {
if (string.IsNullOrWhiteSpace(jbox.TextValue) && string.IsNullOrWhiteSpace(combo.Text)) {
MessageBox.Show("some fields are empty");
} else {
form3.Show();
}
}
Is there a solution where the MessageBox
shows up only once?
You can add a break statement (break;
) to break out of the foreach
loop once you use MessageBox.Show()
. Otherwise you can use a boolean that once the MessageBox
is shown, it turns true
. Then only show the MessageBox
when it's false
.