Click here to Skip to main content
15,848,319 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

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:

C#
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()
        {
            InitializeComponent();

            int n = 1;

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


XML
<Window x:Class="WpfWhileLoop.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TextBox Height="311" HorizontalAlignment="Left" Name="textBox1" VerticalAlignment="Top" Width="503" AcceptsReturn="True" />
</Grid>
</Window>


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?

Thanks!
Posted
Updated 2-Jan-11 13:45pm
v2
Comments
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:

C#
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.
 
Share this answer
 
Hi,

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

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



Valery.
 
Share this answer
 

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