0

I have this silly C# code on a buttonClick (WPF) handler and i wonder why the text box shows only the last value(=0) and no the previous values(9-1) . When I tested the code to a console application I saw all values in the command line.(I am beginner to C#,WPF)

Thnx

private void clock_Click(object sender, RoutedEventArgs e)
{
    DateTime clicktime = DateTime.Now;
    int nextsecont = DateTime.Now.Second + 1;
    int now = clicktime.Second;
    int timer = 10;

    do
    {    
        if (nextsecont == DateTime.Now.Second)
        {
            now++;
            timer--;
            nextsecont++;
            textbox3.Text = timer.ToString();

        }

    } while (nextsecont <= (clicktime.Second + 10));                
}

PS: I know there are better ways to do that (f.e. to use timer) but this is not the point. Sorry for my english..

Explanation: now i see the initial value for 10 seconds and then i see the 0. I don't see intermediate values..

peter-ece
  • 1
  • 1
  • The same reason why a digital clock doesn't show the last 10 minutes: it only shows the current value – cost Nov 24 '14 at 01:09
  • not doing it the 'better way' is exactly the point. you are not using a timer and thus all you do is make your code loop excessively (thus taking up CPU) for 10 seconds. the UI never has the time to refresh. Use a timer and it will work. – Patrick Klug Nov 24 '14 at 01:34

1 Answers1

1

If you want all of the data to display in the textbox, you'll need to append the data to what is already in there.

textbox3.Text += " " + timer.ToString();

That will take what is in there, add a space, and then the timer.

krillgar
  • 12,596
  • 6
  • 50
  • 86
  • i want to display the sequence of the textbox content. like a timer doing – peter-ece Nov 24 '14 at 01:20
  • now i see the initial value for 10 seconds and then i see the 0. I don't see intermediate values.. – peter-ece Nov 24 '14 at 01:24
  • Yes, by doing `textbox3.Text = timer.ToString()`, you're overwriting anything that's in the box every time it updates. By doing `+=`, you'll add the new information at the end of the textbox information. – krillgar Nov 24 '14 at 01:24
  • I'm not sure what you mean by "intermediate values", but I would imagine that they're not in there because you're only adding to the textbox when `nextsecont == DateTime.Now.Second`. You'll have to adjust that algorithm to add the information when else you're looking for. Probably have to separate writing to the textbox and adjusting your variables. – krillgar Nov 24 '14 at 01:27
  • that is the point the textbox content is not changed before event handler elapsed – peter-ece Nov 24 '14 at 01:31
  • Oh. Well that's an issue to do with `Threading`. That's a much larger thing to get into. Check out [this answer](http://stackoverflow.com/a/10715059/1195056) as a way to accomplish that. – krillgar Nov 24 '14 at 01:33
  • thank you for your answer but i want to know the reason why the textbox content is not changed before event handler elapsed . – peter-ece Nov 24 '14 at 10:36