How to display output using for loop in TextBox?

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?

[365 byte] By [huabing78] at [2007-12-25]
# 1

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.

MikeDanes at 2007-9-3 > top of Msdn Tech,Visual C#,Visual C# General...
# 2
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.

sirjis at 2007-9-3 > top of Msdn Tech,Visual C#,Visual C# General...
# 3

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.

huabing78 at 2007-9-3 > top of Msdn Tech,Visual C#,Visual C# General...
# 4

Try "\r\n" or better

Environment.NewLine

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

MikeDanes at 2007-9-3 > top of Msdn Tech,Visual C#,Visual C# General...
# 5
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

huabing78 at 2007-9-3 > top of Msdn Tech,Visual C#,Visual C# General...
# 6

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;

}

}

MikeDanes at 2007-9-3 > top of Msdn Tech,Visual C#,Visual C# General...
# 7

You should also consider what sirjis said above about using a StrinBuilder rather than appending directly to the textbox. It's more efficient.

MikeDanes at 2007-9-3 > top of Msdn Tech,Visual C#,Visual C# General...
# 8

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~!

huabing78 at 2007-9-3 > top of Msdn Tech,Visual C#,Visual C# General...