Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i can iterate through the controls which are on panel by this two code
Form4 fl = new Form4();
           StringBuilder sb = new StringBuilder();
           foreach (Control c in panel1.Controls)
           {
               if (c is ComboBox)
               {
                   ComboBox cb = (ComboBox)c;
                   sb.Append(cb.Text);
                   fl.comboBox1.Text = sb.ToString();
                   fl.Show();
               }
               
           } 
OR by this
List<combobox> lst = new List<combobox>();
void GetComboBoxValues()
{
    StringBuilder sb = new StringBuilder();
    foreach (ComboBox c in lst)
    {
        sb.Append(c.Text + "\r\n");
    }
    MessageBox.Show(sb.ToString());
}

but i add a panel and on panel a usercontrol which contains a combobox and textbox how that can be possible to find controls and add to the string builder
Posted

1 solution

You need to expose those Controls ( ComboBox,TextBox etc) inside your user control as Public properties so that you can access these control just like the other properties of the UserControl.

eg:

class MyUserControl :UserControl
{
  public TextBox MyTextBox
  {
    get {return textBox1;}
  }
 
  public ComboBox MyComboBox
  {
    get { return comboBox1;}
  }
}

//Form Class
foreach(Control ctrl in panel1.Controls)
{ 
  if(ctrl is MyUserControl)
  { 
     MyUserControl myCtrl = ctrl as MyUserControl;
     //myCtrl.TextBox.Text
     //myCtrl.ComboBox.Text //????Hope you get this, 
  }
}
<pre>
 
Share this answer
 
Comments
sariqkhan 17-Dec-12 5:18am    
+5

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