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?
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?
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.
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.
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.
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 textboxtxtResults.Clear();
// Load XML fileXmlDataDocument xDoc = new XmlDataDocument();xDoc.Load(txtLook.Text); // txtLook.Text = productcatalog.xml
// Retrieve all piecesXmlNodeList xmlList = xDoc.GetElementsByTagName(txtTag.Text); // txtTag.Text = product// Display each pieceforeach (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
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;
}
}
You should also consider what sirjis said above about using a StrinBuilder rather than appending directly to the textbox. It's more efficient.
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~!![]()