none
How to display output using for loop in TextBox?

    Question

  • A simple question:

    for (int i = 0; i < 10; i++)

    {

    textbox1.Text = i.ToString();

    }

     

    I want to display

    1 2 3 4 ...9 in textbox. However, the codes above only displays 9. Can anyone help?

     

    Monday, October 09, 2006 6:26 AM

Answers

  • At each for loop iteration you are changing all the text in the TextBox. Try doing something like:

    for (int i = 0; i < 10; i++)

    {

    textBox1.Text += i.ToString();
    }

    to "append" text to the textbox.

     

    Monday, October 09, 2006 6:33 AM
  • Each time through the loop, you're setting the value of textbox1.Text to the new value instead of appending it. Use
    textbox1.Text += i.ToString() + " ";
    instead. The + " " will make there be spaces between each number (and after the last one).

    If you're going to be doing much more than this, consider appending all of the strings separately in a StringBuilder and when you are done, setting the textbox1.Text property to the resulting string.

    Edit: Mike, looks like you beat me to it.
    Monday, October 09, 2006 6:36 AM
  • Try "\r\n" or better

    Environment.NewLine

    textBox1.Text += i.ToString() + Environment.NewLine;

     

    Monday, October 09, 2006 6:46 AM
  • Here's what the documentation for XmlNode.InnerText property says:

    "Gets or sets the concatenated values of the node and all its child nodes. "

    (http://msdn2.microsoft.com/en-us/library/system.xml.xmlnode.innertext.aspx)

    So you are getting the <product>  nodes and the InnerText property will return all the text from them in one line. That's way you get two lines, one if the first product details and another one for the second product details.

    To make it work right one solution would be to iterate over the children of the product node:

    foreach (XmlNode xNode in xmlList)

    {

    foreach (XmlNode xChild in xNode.ChildNodes)

    {

    txtResults.Text += xChild.InnerText + " " + Environment.NewLine;

    }

    }

    Monday, October 09, 2006 7:25 AM

All replies

  • At each for loop iteration you are changing all the text in the TextBox. Try doing something like:

    for (int i = 0; i < 10; i++)

    {

    textBox1.Text += i.ToString();
    }

    to "append" text to the textbox.

     

    Monday, October 09, 2006 6:33 AM
  • Each time through the loop, you're setting the value of textbox1.Text to the new value instead of appending it. Use
    textbox1.Text += i.ToString() + " ";
    instead. The + " " will make there be spaces between each number (and after the last one).

    If you're going to be doing much more than this, consider appending all of the strings separately in a StringBuilder and when you are done, setting the textbox1.Text property to the resulting string.

    Edit: Mike, looks like you beat me to it.
    Monday, October 09, 2006 6:36 AM
  • Got it ! Thanks, Mike. Very clear explaination

    Another simple one:

    How to display each numbers at different lines?

    like:

    1

    2

    3

    4

    .....

     

    I tried to use "\n", but didn't work.

    Monday, October 09, 2006 6:43 AM
  • Try "\r\n" or better

    Environment.NewLine

    textBox1.Text += i.ToString() + Environment.NewLine;

     

    Monday, October 09, 2006 6:46 AM
  •  Mike Danes wrote:

    Try "\r\n" or better

    Environment.NewLine

    textBox1.Text += i.ToString() + Environment.NewLine;

     

     

    Thanks again, Mike. appreciate it.

    It worked well.

    However, when I tried your method to display the XML nodes in textbox. It didn't work.

     

    The XML file (productcatalog.xml)

    <?xml version="1.0" ?>

    <productCatalog>
        <catalogName>Jones and Jones Unique Catalog 2004</catalogName>
        <expiryDate>2005-01-01</expiryDate>

        <products>
            <product id="1001">
                <productName>Gourmet Coffee</productName>
                <description>The finest beans from rare Chilean plantations.</description>
                <productPrice>0.99</productPrice>
                <inStock>true</inStock>
            </product>
            <product id="1002">
                <productName>Blue China Tea Pot</productName>
                <description>A trendy update for tea drinkers.</description>
                <productPrice>102.99</productPrice>
                <inStock>true</inStock>
            </product>
        </products>
    </productCatalog>

     

    The codes:

    private void button2_Click(object sender, EventArgs e)

    {

    // Clear the textbox

    txtResults.Clear();

    // Load XML file

    XmlDataDocument xDoc = new XmlDataDocument();

    xDoc.Load(txtLook.Text); // txtLook.Text = productcatalog.xml

    // Retrieve all pieces

    XmlNodeList xmlList = xDoc.GetElementsByTagName(txtTag.Text); // txtTag.Text = product

    // Display each piece

    foreach (XmlNode xNode in xmlList)

    {

    txtResults.Text += xNode.InnerText + " " + Environment.NewLine;

    }

    The result is:

    Gourmet CoffeeThe finest beans from rare Chilean plantations.0.99true
    Blue China Tea PotA trendy update for tea drinkers.102.99true

    But I expected it to be:

    Gourmet Coffee

    The finest beans from rare Chilean plantations.

    0.99

    .....

     

    Thanks

    Monday, October 09, 2006 7:13 AM
  • Here's what the documentation for XmlNode.InnerText property says:

    "Gets or sets the concatenated values of the node and all its child nodes. "

    (http://msdn2.microsoft.com/en-us/library/system.xml.xmlnode.innertext.aspx)

    So you are getting the <product>  nodes and the InnerText property will return all the text from them in one line. That's way you get two lines, one if the first product details and another one for the second product details.

    To make it work right one solution would be to iterate over the children of the product node:

    foreach (XmlNode xNode in xmlList)

    {

    foreach (XmlNode xChild in xNode.ChildNodes)

    {

    txtResults.Text += xChild.InnerText + " " + Environment.NewLine;

    }

    }

    Monday, October 09, 2006 7:25 AM
  • You should also consider what sirjis said above about using a StrinBuilder rather than appending directly to the textbox. It's more efficient.

     

    Monday, October 09, 2006 7:26 AM
  • Thanks, Mike and Sirjis, for poiting out the beautiful solutions to my questions.
    Yeah, I will look into stringbuilder to find out a better solution.

    See you guys around~!

     

    Monday, October 09, 2006 7:48 AM