Click here to Skip to main content
16,021,823 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
i have added a usercontrol dynamically by this code to a panel
C#
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 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
C#
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
C#
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
Posted
Updated 16-Dec-12 14:04pm
v3

1 solution

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 ?
 
Share this answer
 
Comments
shaikh-adil 16-Dec-12 23:35pm    
is this not correct? it this not looking for combobox in usercontrol?
UserControl1 uss = new UserControl1();
public bool validation()
{
if (uss.comboBox1.Text == string.Empty)
{
return true;
}

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