1

I would need some advice or ideas on the problem I'm facing.

I would like to to create a textbox dynamically from an input string like example '11211'.

When it read the first character based on the string above, in this case '1', a textbox with background color yellow would be created.

The loop would continue creating textboxes horizontally till finished reading all the characters in string above.

Snippet of the code is as below :-

foreach (XmlNode xn in list1)

{

       string name = xn["BinCode"].InnerText;

       disGood.Text = name;

       string input = name;

       disOccupied.Text = input.Length.ToString();

       char[] b = name.ToCharArray();

       foreach (char c in b)
       {

           TextBox[] theTextBoxes = new TextBox[1];

           for (int i = 0; i < theTextBoxes.Length; i++)
           {
               theTextBoxes[i] = new TextBox();

               if (c.ToString() == "1")
               {
                   theTextBoxes[i].BackColor = Color.Yellow;
               }

           }

            disGood.Text = c.ToString();
        }
}

Some sample or ideas would be high recommended.

Thank you.

2
  • 2
    this.Controls.Add(theTextBoxes[i]) Commented Oct 23, 2014 at 7:28
  • User2012384 - Im geting this error - Control 'ctl02' of type 'TextBox' must be placed inside a form tag with runat=server. Commented Oct 23, 2014 at 7:38

2 Answers 2

1

I see a lot of redundancies. For example: your array of textboxes is useless, since you always create one textbox during each iteration. The assignment to the extra variable input and b are extra, but unneeded operations. Try this:

foreach (XmlNode xn in list1)
{
    string name = xn["BinCode"].InnerText;
    disGood.Text = name;
    disOccupied.Text = name.Length.ToString();
    foreach (char c in name) // You can iterate through a string.
    {
        TextBox theTextBox = new TextBox();
        if (c == '1') // Compare characters.
        {
            theTextBox.BackColor = Color.Yellow;
        }
        Controls.Add(theTextBox); // Add the textbox to the controls collection of this parent control.
        disGood.Text = c.ToString(); // This will only show the last charachter. Is this as needed?
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Martin, I did removed the redundancies and tried running the method you mentioned. It works too. Thank you. :)
0

To dynamically create and display textbox's horizontally use the below code:

  TextBox t = new TextBox()
    {
         //To display textbox horizontally use the TableLayoutPanel object
         TableLayoutPanel(TextBox, Column, Row);
         //add any properties specs,
         //more property specs,
    };

1 Comment

Found how to display textboxes horizontally. When adding them to the TableLayoutPanel Control simply specify the column and row. TableLayoutPanel(TextBox, Column, Row); Easy :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.