0

我已经动态添加了一个用户控件到一个winform面板我已经动态添加了控件所以我想验证winform中的控件所以我使用了这个方法

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

在我使用的按钮单击事件中

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

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

除了这样,我还使用此代码来显示哪个控件未正确填充,但它仅将第一个控件显示为错误我是编程新手,所以我在这里有点困惑

         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");
                        }

                    }

                }
            }
        }
    }
4

1 回答 1

1

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");
                    }
于 2012-12-17T15:05:39.030 回答