C# 动态验证添加的用户控件
我已将usercontrol动态添加到winform的面板中 我已经动态添加了控件,所以我想在winform中验证控件,所以我使用了这个方法C# 动态验证添加的用户控件,c#,winforms,C#,Winforms,我已将usercontrol动态添加到winform的面板中 我已经动态添加了控件,所以我想在winform中验证控件,所以我使用了这个方法 private bool mobilemanu() { return panel1.Controls.OfType<UserControl1>().Select(uc => uc.comboBox1).Any(cb => cb.Text == String.Empty); } 此外,我还使用此
private bool mobilemanu()
{
return panel1.Controls.OfType<UserControl1>().Select(uc => uc.comboBox1).Any(cb => cb.Text == String.Empty);
}
此外,我还使用此代码显示未正确填充的控件
但它只是将第一个控件显示为错误
我是一个编程新手,所以我在这里有点困惑
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");
}
}
}
}
}
}
也许您只是混淆了控件,因为它们的名称只在名称的最后一个字母(数字)不同?这是否有效:
if (myCrl.comboBox1.Text == string.Empty)
{
errorProvider1.SetError(myCrl.comboBox1, "entersomething");
}
if (myCrl.comboBox2.Text == string.Empty)
{
errorProvider1.SetError(myCrl.comboBox2, "entersomething");
}
是的,这是一个愚蠢的错误,Iavn没有看到,对不起,我你没有发行版问题,类似的事情发生了:)
if (myCrl.comboBox1.Text == string.Empty)
{
errorProvider1.SetError(myCrl.comboBox1, "entersomething");
}
if (myCrl.comboBox2.Text == string.Empty)
{
errorProvider1.SetError(myCrl.comboBox2, "entersomething");
}