Click here to Skip to main content
15,911,646 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have added usercontrol dynamically so i want to validate it in simple way i used this for validation but it is not going well
how to have validation in bool in simple way
C#
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");

            }

        }

all the time i click button the form shows up, my code is not examining the error,how to do that
Posted
Comments
Jibesh 17-Dec-12 3:44am    
did you debug your code?

Do you know how to debug?
sariqkhan 17-Dec-12 3:55am    
no, sir OG have given some info but i dont know clearly to bedug, maine kabi khudse nahi kiya hai

The problem is that your validation method returns only the last result - if the final combobox in the list is not empty, it will return false.
Try break on failure, or return

BTW: Did you know you can do the test a lot easier and more accurately?
C#
public bool val2()
{
    foreach (Control item in panel1.Controls.OfType<ComboBox>())
    {
        if (string.IsNullOrWhiteSpace(item.Text))
        {
            return true;
        }
    }
    return false
}


Oh, and tat should not be a class level variable - if you are going to do it your way, tat should be local to the method, not declared outside it.
 
Share this answer
 
Comments
sariqkhan 17-Dec-12 3:53am    
your solution is also opening the form when the button is click,
means it is not working properly,
int c = 0;
private void button1_Click(object sender, EventArgs e)
{
int v;
v = c++;
panel1.VerticalScroll.Value = VerticalScroll.Minimum;


UserControl1 us = new UserControl1();
us.Name = "us" + v;
us.Location = new Point(150, 5 + (30 * v));

panel1.Controls.Add(us);

}
i added the user control by this code,is this posible to validate by your code?
OriginalGriff 17-Dec-12 3:57am    
Then you need to look at the number of objects as well - your version would have returned the previous value!
sariqkhan 17-Dec-12 4:21am    
sir,if i use
public bool val3()
{
Regex regex = new Regex("^[a-z]$");
return panel1.Controls.OfType<usercontrol1>().Select(uc => uc.comboBox1).Any(cb => !regex.IsMatch(cb.Text));
}
and
at the click event
private void button2_Click(object sender, EventArgs e)
{
bool val21 = val3();
if (!val21)
{
Form4 fp = new Form4();
fp.Show();
}
else
{
MessageBox.Show("error");

}
}
it shows error all the time why sir?
OriginalGriff 17-Dec-12 4:31am    
Because your regex matches only strings which consist of only a single lowercase character. It does not match blank lines, "A", "B", or "Z", or "ab", "or", "at", or any other string which contains more than a single lowercase character .
sariqkhan 17-Dec-12 5:08am    
i got it it should be like {0,4}
thank you sir
the Method Val will return the value of the member variable tat which is false by default.
also Val will always return false or last set value if panel.Controls doesnt not contain any comboBox type control

More over you are checking for !valo to show your Form and when valo == true you are displaying Error.

correct your code and debug yourself before posting here... it will save too many ppls time.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900