如何动态添加usercontrol时验证它 [英] how to validate a usercontrol when it is added dynamically

查看:72
本文介绍了如何动态添加usercontrol时验证它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已通过此代码动态地将用户控件添加到面板

i have added a usercontrol dynamically by this code to a panel

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));
  us.Tag = btn;
  panel1.Controls.Add(us);
}



i在表单上有一个用于制作动态用户控件的按钮,另一个用于将文本传递给其他表单,然后将其传递给另一个表单i如果usercontrol中的任何控件都留空,想要验证文本并清除传输文本的过程,usercontrol中的控件是组合框和文本框,所以我写了这个方法


i have a two button on the form one for making the dynamic usercontrol and one for passing the text to other form, before passing it to another form i want to validate the text and cancle the process of transferring the text if any control in the usercontrol is left blank,the control in the usercontrol is comboboxes and textbox, so i have writte this method

UserControl1 uss = new UserControl1();
public bool validation()
{
  if (uss.comboBox1.Text == string.Empty)
  {
     return true;
  }
  else
  {
     return false;
  }
}



以下代码位于验证按钮


and the below code is at the validating button

bool vss = validation();
if (!vss)
{
  errorlabel.Visible = false;   //this label is defaultly set to false
  Form4 fl = new Form4(); 
  fl.Show();
}
else
{
 errorlabel.Visible = true;    //this label is defaultly set to false
}
//but the validation is not done properly, whats the problem here

推荐答案

如果动态添加内容,则应创建一个集合来跟踪它们,以便您可以出于某种原因迭代它们。您的验证方法不是在寻找您动态添加的控件,那么它如何验证它们呢?
If you add things dynamically, you should create a collection to keep track of them so you can iterate over them for whatever reason. Your validation method is not looking for controls you added dynamically, so how can it validate them ?


这篇关于如何动态添加usercontrol时验证它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆