shariq_khan
shariq_khan

Reputation: 681

error in validating the bool condition

i have added a usercontrol on a panel and set the property of combobox which is in usercontrol to public, i want to validate that but all the time i click the button it shows the form. whats the problem, how to validate when i have added multiple combobox dynamically by a button.

bool tat;
public bool val2()
{
    foreach (Control item in panel1.Controls.OfType<ComboBox>())
    {
        if (item.Text == string.Empty)
        {
            tat = true;
        }
        else
        {
            tat = false;
        }
    }

    return tat;
}

private void button2_Click(object sender, EventArgs e)
{
    bool valo = val2();

    if (!valo)
    {
        Form4 fp = new Form4();
        fp.Show();
    }
    else
    {
        MessageBox.Show("error");
    }
}

Upvotes: 0

Views: 145

Answers (2)

Rawling
Rawling

Reputation: 50184

What you're doing is overwriting the value of tat each time through your loop, e.g.:

ComboBox1    "Hello"   tat = false
ComboBox2    Empty     tat = true
ComboBox3    "World"   tat = false again!
             Return:   false

I assume you want to return true if any combo box is empty; you can modify your function as follows:

public bool val2()
{
    foreach (Control item in panel1.Controls.OfType<ComboBox>())
    {
        if (item.Text == string.Empty)
        {
            // We know one is empty so we must return true.
            return true;
        }
    }
    // We know none are empty so we can return false.
    return false;
}

Or since you're already using LINQ, you could use

public bool val2()
{
    return panel1.Controls.OfType<ComboBox>().Any(cb => cb.Text == String.Empty);
}

which will do the exact same thing.


Edit: It also looks like your ComboBox controls aren't direct children of panel1, but are inside some sort of custom user control. Thus you'll need something like

return panel1.Controls
    .OfType<YourCustomUserControlType>()
    .Select(uc => uc.NameOfYourComboBox)
    .Any(cb => cb.Text == String.Empty);

So the OfType line is now picking out your custom user controls, the Select line takes one of your user controls and finds the ComboBox, and the Any line checks if they all have values.

Alternative in original style:

public bool val2()
{
    foreach (YourCustomUserControlType item in 
        panel1.Controls.OfType<YourCustomUserControlType>())
    {
        ComboBox cb = item.NameOfYourComboBox;
        if (cb.Text == string.Empty)
        {
            // We know one is empty so we must return true.
            return true;
        }
    }
    // We know none are empty so we can return false.
    return false;
}

Upvotes: 4

Thorsten Dittmar
Thorsten Dittmar

Reputation: 56727

The problem is that you're deciding the value of val2 for every combo box. Probably, the last combo box has a value, so the form is shown instead of the message box.. If you want the result to be true only if all combo boxes have selected values, you need to go for a solution like the following:

Assume that all combo boxes have selected values. As soon as only one doesn't have a value, return true;

public bool val2()
{
    foreach (Control item in panel1.Controls.OfType<ComboBox>())
    {
        if (String.IsNullOrEmpty(item.Text))
            return true;
    }
    return false;
}


private void button2_Click(object sender, EventArgs e)
{

    bool valo = val2();

    if (!valo)
    {
        Form4 fp = new Form4();
        fp.Show();
    }
    else
    {
        MessageBox.Show("error");

    }
}

Upvotes: 2

Related Questions