Click here to Skip to main content
15,867,834 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
hi,i am a student and my friend is facing difficulty in passing the data from one control which is dynamic generated the control which is dynamic generated is by this code
C#
int c = 0;
 int p = 0;
 private void button1_Click(object sender, EventArgs e)
 {
     panel1.VerticalScroll.Value = VerticalScroll.Minimum;
     ComboBox txtRun3 = new ComboBox();
     txtRun3.Name = "txtDynamic" + c++ ;
     txtRun3.Location = new Point(30, 18 + (30 * c))
     panel1.Controls.Add(txtRun3);
 }

and this is the code for transfering the text of the combobox with to the next form4
XML
private void button2_Click(object sender, EventArgs e)
       {
           Form4 f4 = new Form4();
           Button bs = sender as Button;
           f4.Combo = panel1.Controls.OfType<ComboBox>().First().Text;
          f4.Show();

       }



he have written the below propoerty in the public section of the form4
C#
public string Combo
        {
            get
            {
                return this.comboBox1.Text;
            }
            set
            {
                this.comboBox1.Text = value;
            }
        }

Quote:
and the problem is if two or more combobox wil be present the text of the combobox will be transfer to the form4's combobox like
txtRun3.text,txtRun2.text,txtRun3.text
means commas will be there for seperation, how to do that
and the upper code transfer only one value to the next form, how to transfer all the combobox's text to one cpmbobox with comma seperation
Posted
Updated 12-Dec-12 1:00am
v4

You can declare a List to store the value of ComboBox. Example:
C#
public partial class Form1 : Form
{
    List<combobox> lst = new List<combobox>();
    public Form1()
    {
        InitializeComponent();
    }
}

By the time you create ComboBox dynamically, you add it into the List:
C#
void DynamicAddComboBox()
{
    for (int i = 0; i < 3; i++)
    {
        ComboBox c = new ComboBox();
        panel1.Controls.Add(c);
        lst.Add(c);
    }
}

When you want to get the values of the combobox, just loop through the list. Example:
C#
void GetComboBoxValues()
{
    StringBuilder sb = new StringBuilder();
    foreach (ComboBox c in lst)
    {
        sb.Append(c.Text + "\r\n");
    }
    MessageBox.Show(sb.ToString());
}


Update:
You can even loop through the combobox direct within the panel without the help of list. You don't have to add it into List. Example:
C#
void GetComboBoxValuesFromPanel()
{
    StringBuilder sb = new StringBuilder();
    foreach (Control c in panel1.Controls)
    {
        if (c is ComboBox)
        {
            ComboBox cb = (ComboBox)c;
            sb.Append(cb.Text);
        }
    }
    MessageBox.Show(sb.ToString());
}
 
Share this answer
 
v4
Comments
sariqkhan 12-Dec-12 8:36am    
sir, very much thank to you
you have solved mu doubt,
really really thank you
adriancs 12-Dec-12 8:50am    
Hi, I have updated the answer.
sariqkhan 12-Dec-12 8:52am    
how will be the updated answer usefull?
first is with the help of list and second is without the help of list?
right na?
adriancs 12-Dec-12 8:54am    
Which of the line of code that you don't understand?
adriancs 12-Dec-12 8:54am    
Yes, it works.
passing data from dynamically generated cotrol to the other form's control[^]

so far this question been updated, this solution won't work..
-Krunal
 
Share this answer
 
v2
Comments
sariqkhan 12-Dec-12 6:49am    
sorry sir,
question updated
sariqkhan 4-Jan-13 14:52pm    
http://www.codeproject.com/Questions/521783/howplustoplustransferplustheplustextplusfromplusdy
can you help me sir

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