Simple loop display problem

Hi

Newbie in c# programming. I was wondering what was wrong in this code. What I wanted is that I have to show counting on display, however, it only shows the last count. I checked the output and I see my debug.prints… which indeed says that it has entered the loop.

using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Input;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;

namespace FEZ_Cobra_Window_Application4
{
    public class Program : Microsoft.SPOT.Application
    {
        public static void Main()
        {
            Program myApplication = new Program();

            Window mainWindow = myApplication.CreateWindow();

            // Create the object that configures the GPIO pins to buttons.
            GPIOButtonInputProvider inputProvider = new GPIOButtonInputProvider(null);

            // Start the application
            myApplication.Run(mainWindow);
        }

        private Window mainWindow;

        public Window CreateWindow()
        {
            // Create a window object and set its size to the
            // size of the display.
            mainWindow = new Window();
            mainWindow.Height = SystemMetrics.ScreenHeight;
            mainWindow.Width = SystemMetrics.ScreenWidth;

            // Create a single text control.
            Text text = new Text();

            text.Font = Resources.GetFont(Resources.FontResources.small);
            for (int x = 0; x < 10; x++)
            {
                text.TextContent = "Hi User!";
                Thread.Sleep(200);
                text.TextContent = x.ToString();
                Thread.Sleep(200);
            }
          //  text.TextContent = Resources.GetString(Resources.StringResources.String1);
            text.HorizontalAlignment = Microsoft.SPOT.Presentation.HorizontalAlignment.Center;
            text.VerticalAlignment = Microsoft.SPOT.Presentation.VerticalAlignment.Center;

            // Add the text control to the window.
            mainWindow.Child = text;

            // Connect the button handler to all of the buttons.
            mainWindow.AddHandler(Buttons.ButtonUpEvent, new RoutedEventHandler(OnButtonUp), false);

            // Set the window visibility to visible.
            mainWindow.Visibility = Visibility.Visible;

            // Attach the button focus to the window.
            Buttons.Focus(mainWindow);

            return mainWindow;
        }

        private void OnButtonUp(object sender, RoutedEventArgs evt)
        {
            ButtonEventArgs e = (ButtonEventArgs)evt;

            // Print the button code to the Visual Studio output window.
            Debug.Print(e.Button.ToString());
        }
    }
}

Hope you could help me with this.
Thanks! :slight_smile:

Using code tags will make your post more readable. This can be done in two ways:[ol]
Click the “101010” icon and paste your code between the

 tags or...
Select the code within your post and click the "101010" icon.[/ol]
(Generated by QuickReply)

Try changing to…

for (int x = 0; x < 10; x++)
{
    text.TextContent = "Hi User!";
    Thread.Sleep(200);
    text.TextContent = x.ToString();
    text.Invalidate();
    Thread.Sleep(200);      //  You might have to play with extending this out to.  Not sure if it can update that fast.
}

@ Josh - try taking this post and adding it to the bottom of my previous post. Something gets really confused…

same results. :frowning: prints 9 after execution. didnt show countdown nor the Hi User.

You cant change the contents of the from while you are creating it. The form is invisible till near the end where is it set to visible.

The simplest way to do this would be to add a timer that runs every 400ms to update the screen…

As GMod says, you should put the loop AFTER this code:

       // Set the window visibility to visible.
            mainWindow.Visibility = Visibility.Visible;

Is the loop just a timer loop where ill define my timer variable and duration? If I have to put the loop after the visibility line, then how should I “bring the execution up”? (need to do this since I have to update the text itself.

Sorry dude, no. The loop may well work the way you have shown it, just reordered after the mainWindow.Visibility setting. EDIT: And to be clear, use the FOR loop that Ian proposed you used a few posts ago.

The Timer approach would be another way to call the loop based on an actual duration, rather than sleeping for a defined time. Roughly the same result, but a different way of approaching this.

So you mentioned you were a newbie; it’s really worth going back through the tutorials from the Support – GHI Electronics page to get some simple stuff under your belt - the Resources tab has the beginners guide to the .Net Micro Framework that is a great resource, as well as the individual items in the Netmf Tutorials tab.