简体   繁体   中英

Validate dynamically added UserControl

i have added a usercontrol dynamically to a panel of winform i have added the control dynamically so i want to validate the control in the winform so i have used this method

   private bool mobilemanu()
    {
        return panel1.Controls.OfType<UserControl1>().Select(uc => uc.comboBox1).Any(cb => cb.Text == String.Empty);
    }

and in the button click event i used

       private void button2_Click(object sender, EventArgs e)
    {
        bool mobil = mobilemanu();

        if (!mobil)
{
    //do this
}
    else
{
    //do that
}
}

also along with such i used this code for showing which control is not filled properly but it is only showing first control as errored i am a newbie to programming so i am little bit confused here

         private void mobilemanuval()
    {
        bool val = mobilemanu();
        if (val == true)
        {
            foreach (Control ctrl in panel1.Controls)
            {
                if (ctrl is UserControl1)
                {
                    UserControl1 myCrl = ctrl as UserControl1;

                    {
                        if (myCrl.comboBox2.Text == string.Empty)
                        {
                            errorProvider1.SetError(myCrl.comboBox1, "entersomething");
                        }
                        if (myCrl.comboBox2.Text == string.Empty)
                        {
                            errorProvider1.SetError(myCrl.comboBox2, "entersomething");
                        }

                    }

                }
            }
        }
    }

Maybe you just mixed up controls because their names only differ in last letter of the name (digit)? Does this work:

                    if (myCrl.comboBox1.Text == string.Empty)
                    {
                        errorProvider1.SetError(myCrl.comboBox1, "entersomething");
                    }
                    if (myCrl.comboBox2.Text == string.Empty)
                    {
                        errorProvider1.SetError(myCrl.comboBox2, "entersomething");
                    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM