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++;
}
}
}
}
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.
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();
Last edited by BigEd781; January 2nd, 2011 at 11:50 PM.
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
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.