-
1 Attachment(s)
WPF Iteration
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:
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++;
}
}
}
}
<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!
-
Re: WPF Iteration
You are changing the text property in each iteration, not sure why you would expect the old value to be maintained (and also for a newline to be added).
You need to add a line in each iteration, not completely wipe out the Text property. You can either concatenate to the Text or add to the Lines collection.
-
Re: WPF Iteration
Yup, that makes sense now that I look a little closer :)
Here's the solution I came up with:
textBox1.Text += "current value of n is " + n++ + Environment.NewLine;
Thanks for the pointer!
-
Re: WPF Iteration
Yep, that works. However, if it were me I would do this:
Code:
int i = 1;
List<string> lines = new List<string>();
while( i < 6 )
{
lines.Add( String.Format( "current value of n is {0}", n++ ) );
}
textbox1.Lines = lines.ToArray();
-
Re: WPF Iteration
I like it but I pasted you code snippet and got an error on the texBox1.Lines method. Should this work or am I missing something?
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++ + Environment.NewLine;
//}
int i = 1;
List<string> lines = new List<string>();
while (i < 6)
{
lines.Add(String.Format("current value of n is {0}", i));
}
textBox1.Lines = lines.ToArray();
}
}
}
Error 1 'System.Windows.Controls.TextBox' does not contain a definition for 'Lines' and no extension method 'Lines' accepting a first argument of type 'System.Windows.Controls.TextBox' could be found (are you missing a using directive or an assembly reference?) C:\Users\shawn\AppData\Local\Temporary Projects\WpfWhileLoop\MainWindow.xaml.cs 43 22 WpfWhileLoop
-
Re: WPF Iteration
Sorry, I don't use WPF. For the System.Windows.Controls.TextBox class you would do it this way.
Code:
int i = 1;
while( i < 6 )
{
textbox1.AppendText( String.Format( "current value of n is {0}{1}", n++, Environment.NewLine ) );
}
-
Re: WPF Iteration
Lines is a member of System.Windows.Forms.TextBox, not System.Windows.Controls.TextBox.
This is why I haven't bothered with WPF much to date. It irks me that WPF classes designed completely different from WinForms.
-
Re: WPF Iteration
Cool, thanks for the feedback! Final solution that supported by WPF:
Code:
int i = 1;
while (i < 6)
{
textBox1.AppendText(String.Format("current value of n is {0}{1}", i++, Environment.NewLine));
}