WPF Iteration
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: WPF Iteration

  1. #1
    Join Date
    Jan 2011
    Posts
    4

    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!
    Attached Images Attached Images  

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    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.

  3. #3
    Join Date
    Jan 2011
    Posts
    4

    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!

  4. #4
    Join Date
    Jun 2008
    Posts
    2,477

    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();
    Last edited by BigEd781; January 2nd, 2011 at 11:50 PM.

  5. #5
    Join Date
    Jan 2011
    Posts
    4

    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

  6. #6
    Join Date
    Jun 2008
    Posts
    2,477

    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 ) );
    }
    Last edited by BigEd781; January 2nd, 2011 at 11:49 PM.

  7. #7
    Join Date
    Aug 2008
    Posts
    902

    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.

  8. #8
    Join Date
    Jan 2011
    Posts
    4

    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));
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center