Click here to Skip to main content
14,699,048 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

Using .net 4 & VS C# Express 2010, new to both and I'm working through some basic programming tasks.

I'm trying to have a while loop count disaplyed in my WFP textbox which is easy enough to do in a wpf console.

Here's my code and xaml:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfWhileLoop
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
        public MainWindow()

            int n = 1;

            while (n < 6)
                textBox1.Text = "current value of n is " + n++;

<Window x:Class="WpfWhileLoop.MainWindow"



Title="MainWindow" Height="350" Width="525">
<TextBox Height="311" HorizontalAlignment="Left" Name="textBox1" VerticalAlignment="Top" Width="503" AcceptsReturn="True" />

When I run this I get:

current value of n is 5

I 'm trying to have each increment displayed, e.g.:

current value of n is 1
current value of n is 2
current value of n is 3
current value of n is 4
current value of n is 5

Any ideas how i can make this work?

Updated 2-Jan-11 13:45pm
Tony Richards 2-Jan-11 18:47pm
Edited to fix code snippets. In future, please wrap the code snippets in pre tags, but not the rest of the text (as I have done here). It makes it clearer, preserves indentation, and a clearer question is more likely to get a reply.

The simplest way to do it is probably as follows:

textBox1.Text += "current value of n is " + (n++) + Environment.NewLine;

This appends the string to new value to the current value of the Text property.

I'm assuming this is a simplified example. If you're actual requirement is more complex, you may want to look at binding the list of values to something like a ListBox and using data templates to format it appropriately. This would be a more normal 'WPF' way of doing things.

You may want to consider looking through a C# for beginners book. This is a fairly basic string manipulation task, and you may benefit from working through the basics.

You just need to add a new line in each iteration.

while (n < 6)
    textBox1.Text = textBox1.Text + "current value of n is " + n++ + Environment.NewLine;


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900