Generating Dynamic Controls in C# windows Application
<p>My GUI looks like the one below. 
<a href="http://i.stack.imgur.com/mDvJq.png" rel="nofollow">GUI Image</a></p>

<p>On click of generate Button I am taking all the inputs from the GUI and creating and xml file for it.
My Code to generate Xml file below</p>

<pre><code>namespace ActionSample
{
    [Serializable()]
    public class TestCase
    {
        [XmlElement(ElementName = "TestCaseID")]
        public string TestCaseID { get; set; }
        [XmlElement(ElementName = "TestCaseDescription")]
        public string TestCaseDescription { get; set; }
        // public Action Action;
        [XmlElement(ElementName = "TestSetup")]
        public TestSetup TestSetup { get; set; }
        [XmlElement(ElementName = "TestExecution")]
        public TestExecution TestExecution { get; set; }
    }

    public class TestSetup
    {
        [XmlElement(ElementName = "Action")]
        public Action Action { get; set; }
    }

    [XmlRoot(ElementName = "TestStep")]
    public class TestStep
    {
        [XmlElement(ElementName = "IsExecutable")]
        public bool IsExecutable { get; set; }
        [XmlElement(ElementName = "RequestEndpoint")]
        public string RequestEndpoint { get; set; }
        [XmlElement(ElementName = "Action")]
        public Action Action { get; set; }
    }

    [XmlRoot(ElementName = "TestExecution")]
    public class TestExecution
    {
        [XmlElement(ElementName = "TestStep")]
        public TestStep TestStep { get; set; }
    }

    public class Action
    {
        public string ActionCommand;
        public string TimeOut;
        public string BamSymbol;
        public string Side;
        public string PrimeBroker;
        public string Size;
        public string PMCode;
        public string Strategy;
        public string SubStrategy;
        public string ActionLogEndPoint;
        public string ParameterName;
        public string ParameterValue;
        public bool IsActionResultLogged;
        [XmlElement(ElementName = "ValidationStep")]
        public ValidationStep ValidationStep { get; set; }
    }

    [XmlRoot(ElementName = "ValidationStep")]
    public class ValidationStep
    {
        [XmlElement(ElementName = "IsValidated")]
        public bool IsValidated { get; set; }
        [XmlElement(ElementName = "ValidationFormat")]
        public string ValidationFormat { get; set; }
        [XmlElement(ElementName = "ResponseEndpoint")]
        public string ResponseEndpoint { get; set; }
        [XmlElement(ElementName = "ResponseParameterName")]
        public string ResponseParameterName { get; set; }
        [XmlElement(ElementName = "ResponseParameterValue")]
        public string ResponseParameterValue { get; set; }
        [XmlElement(ElementName = "ExpectedValue")]
        public string ExpectedValue { get; set; }
        [XmlElement(ElementName = "IsValidationResultLogged")]
        public bool IsValidationResultLogged { get; set; }
        [XmlElement(ElementName = "ValidationLogEndpoint")]
        public string ValidationLogEndpoint { get; set; }
    }
}
</code></pre>

<p>Form1.cs</p>

<pre><code>public void button1_Click(object sender, EventArgs e)
    {
        XmlSerializer serializer = new XmlSerializer(typeof(TestCase));            
        StringWriter sw = new StringWriter();
        XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
        ns.Add(string.Empty, string.Empty);
        XmlTextWriter writer = new XmlTextWriter("Action.xml", System.Text.Encoding.UTF8);
        writer.WriteStartDocument(true);
        writer.Formatting = Formatting.Indented;
        writer.Indentation = 2;

        TestCase tc = new TestCase();
        Action actionObj = new Action();
        TestSetup testsetupObj = new TestSetup();
        ValidationStep vsObj = new ValidationStep();
        TestStep teststepObj = new TestStep();
        TestExecution teObj = new TestExecution();

        actionObj.ActionCommand = comboBox1.Text.ToString();
        actionObj.TimeOut = textBox1.Text.ToString();
        actionObj.BamSymbol = textBox2.Text.ToString();
        actionObj.Side = comboBox4.Text.ToString();
        actionObj.PrimeBroker = comboBox5.Text.ToString();
        actionObj.Size = textBox5.Text.ToString();
        actionObj.PMCode = comboBox2.Text.ToString();
        actionObj.Strategy = textBox7.Text.ToString();
        actionObj.SubStrategy = comboBox3.Text.ToString();
        actionObj.ActionLogEndPoint = textBox9.Text.ToString();

        tc.TestCaseID = textBox4.Text.ToString();
        tc.TestCaseDescription = textBox6.Text.ToString();
        //tc.Action = actionObj;

        teststepObj.IsExecutable = Convert.ToBoolean(textBox17.Text.ToString());
        teststepObj.RequestEndpoint = textBox18.Text.ToString();

        vsObj.IsValidated = Convert.ToBoolean(textBox8.Text.ToString());
        vsObj.ValidationFormat = textBox10.Text.ToString();
        vsObj.ResponseEndpoint = textBox11.Text.ToString();
        vsObj.ResponseParameterName = textBox12.Text.ToString();
        vsObj.ResponseParameterValue = textBox13.Text.ToString();
        vsObj.ExpectedValue = textBox14.Text.ToString();
        vsObj.IsValidationResultLogged = Convert.ToBoolean(textBox15.Text.ToString());
        vsObj.ValidationLogEndpoint = textBox16.Text.ToString();
        actionObj.ValidationStep = vsObj;
        testsetupObj.Action = actionObj;
        teststepObj.Action = actionObj;
        teObj.TestStep = teststepObj;
        tc.TestSetup = testsetupObj;
        tc.TestExecution = teObj;
        serializer.Serialize(writer, tc);
        writer.Close();          
        MessageBox.Show("XML File created ! ");
    }
</code></pre>

<p>Now I have a requirememnt like . 
Case I
1.On click of Clone button all the controls which are grouped under Testsetup should be dynamically generated.
For which I tried to generate only two controls dynamically and code is pasted below.</p>

<pre><code>int inputNumber = 1;

        //Initialize list of input text boxes
        inputTextBoxes = new List&lt;TextBox&gt;();
        inputComboBoxes = new List&lt;ComboBox&gt;();
        string dd;

        //Generate labels and text boxes
        for (int i = 1; i &lt;= inputNumber; i++)
        {
            #region
            //Create a new label and text box
            Label labelInput = new Label();

            ComboBox comboBoxNewInput = new ComboBox();

            //Initialize label's property
            labelInput.Text = "Action";
            labelInput.Location = new Point(30,  groupBox2.Bottom+ (i * 30));
            labelInput.AutoSize = true;

            //Initialize comboBoxes Property
            comboBoxNewInput.Location = new Point(labelInput.Width, labelInput.Top - 3);

            //Add the newly created text box to the list of input text boxes
            inputComboBoxes.Add(comboBoxNewInput);                   

            //Add the labels and text box to the form
            this.Controls.Add(labelInput);

            this.Controls.Add(comboBoxNewInput);

            Label labelInput2 = new Label();
            TextBox textBoxNewInput = new TextBox();

            labelInput2.Text = "TimeOut";
            labelInput2.Location = new Point(comboBoxNewInput.Width + 120, groupBox2.Bottom + (i * 30));
            labelInput2.AutoSize = true;

            //Initialize textBoxes Property
            textBoxNewInput.Location = new Point(labelInput2.Width + 200, labelInput2.Top - 3);

            ////Add the newly created text box to the list of input text boxes
            inputTextBoxes.Add(textBoxNewInput);


            //Add the labels and text box to the form
            this.Controls.Add(labelInput2);
            this.Controls.Add(textBoxNewInput);
</code></pre>

<p>Am I taking the right approach because I find this approach a little tedious. Is there any better way to accomplish my task??</p>

<p>Case II
2. As you can see from TestCase class above code I have created classes and generating xml out of objects of classes.
If the controls are gnerated dynalmically then how should I modify my above code of XmlSerializer?</p>

<p>I treid to search alot about Dynamically generated controls in widows application. Didn't find any luck in getting the good one</p>

<p>Please do help me with suggestions. Any help would be appreciated. Thanks in Advance.</p>